J2se basics: 2. Object creation and use

Source: Internet
Author: User
1: Pass the parameter value and reference


A: Value Transfer: basic data type transfer is a value transfer.

B: Reference transfer (Address Transfer): All data types of objects are passed by reference.




2: class variables and member variables (instance variables, object variables)


Class variable: called by class name. class variables are shared by all instances.

Final Static int max = 20; // defined constant in Java



Object variable: called by an object (the object must be new ).






3: class methods and member methods (instance methods, object methods)

Class Method: called by class name. This keyword cannot be used in class methods.

This indicates the current object.

Member method: Call through an object (the object must be new ).




4:
Constructor
Destroy method (finalize)

The destruction method is called when the object is destroyed.


Java virtual
This object is considered useless by the parsers.



The garbage collector is used to recycle objects allocated in the heap area. The garbage collector only recycles three times
Memory. The garbage collector is automatically called by virtual machines. (Called when the heap memory is insufficient)

However, you can use system. GC () to forcibly run the garbage collector.


5: static block and object Block


Find the main method ---> load class ---> load the static block code of the class (only initialize once)

---> Load Static methods and static variables of the class (only initialize once) ----> Object Block Method

---> Object constructor ---> call object method ---> execute object destruction method.


// Score class score {int English; int math; int Chinese; score () {} score (INT English, int math, int Chinese) {This. english = English; this. math = math; this. chinese = Chinese ;}} class student {int stuid; string stuname; string stusex; // use the score class as an attribute of the student class. Score score; Public student (score) {This. score = score;} public int gettotalscore () {return this. score. english + this. score. math + this. score. chinese;} public void changescore (score) {score. chinese = 0; score. math = 0 ;}} public class test_02 {public static void main (string ARGs []) {score score_one = new score (70, 60, 65); // score_one.english = 70; // score = 60; // score_one.chinese = 65; score score_two = new score (); score_two.english = 11; score = 12; score_two.chinese = 13; score score_three = new score (45, 46, 47); // score_three.english = 45; // bytes = 46; // score_three.chinese = 47; Student stu_one = new student (score_three); Student stu_two = new student (score_two ); student stu_three = new student (score_one);/* system. out. println (stu_one.gettotalscore (); system. out. println (stu_two.gettotalscore (); score_three.english = 70; stu_one.score.math = 23; system. out. println (stu_one.gettotalscore (); system. out. println (stu_two.gettotalscore (); */system. out. println (stu_one.gettotalscore (); // 138stu_one.changescore (score_two); system. out. println (stu_one.gettotalscore (); // 138stu_one.changescore (score_three); system. out. println (stu_one.gettotalscore (); // 45system. out. println (stu_two.gettotalscore ());}}

public class Test_03{int id;final static int MAX = 20;public static void main(String args[]){//Test_03 test = new Test_03();//System.out.println(test.MAX);System.out.println(Test_03.MAX);}}

Class person {int personid; string personname; Public Person () {system. out. println ("object constructor"); this. personid = 1; this. personname = "Chinese";} public void method () {system. out. println ("execution method");} public void finalize () {system. out. println ("the object has been destroyed"); this. personid = 0; this. personname = NULL ;}} public class test_04 {public static void main (string ARGs []) {two (); system. GC ();} public static void two () {// create object person = new person (); // use object person. method ();}}

Public class test_05 {// The first initialization content to be executed when the class is loaded. Static {system. Out. println ("static block");} // when the class is loaded, both static methods and static variables are placed in the static area of the memory. Public static void staticmethod () {system. out. println ("static method");} // content of the object block, the content executed before the object initialization {system. out. println ("Object Block Method");} // Object Construction Method public test_05 () {system. out. println ("constructor");} public void objectmethod () {system. out. println ("Object method");} public static void main (string ARGs []) {test_05.staticmethod (); test_05 test = NULL; test = new test_05 (); test. objectmethod (); test_05 Test2 = NULL; Test2 = new test_05 (); test2.objectmethod () ;}} Class Two {static {system. out. println ("static two block ");}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.