We all know that creating a new object requires the new keyword and the class name you want to create, such as: Person P1 = New Person (); // "=" The left side is defined as the variable PER as the variable type, come Pointing the instance object of a Person class created by the new keyword, the variable P1 is the reference handle of the object, and the reference handler of the object is a variable allocated in the stack. The object itself is allocated in the heap; in New Be sure to follow a pair of parentheses after the class name of the statement (), which is executed after the completion is shown in Figure 1. Stack memory reable memory | --- | ________ | | | | _______ | -> 0x3000 | Age // "0" is the object letter generated by new person () __ | | |. ....... | Number | | _______ | - |. ........ | P1 | 0x3000 | - |. ....... | Stack | --- | | | | Space Figure 1 variables cannot be used before being initialized, and the variables inside one method must be initialized, otherwise the compilation cannot be passed. When an object is created, the initialization assignment of various types of member variables is automatically performed in Table 1, except that variable types other than the basic data type are reference types, such as Person, and previous speakers.
Table 1 Member variable type initial value ----------------------------------------- ------------------------------------------- Byte 0 Short 0 INT 0 Long 0L float 0.0f double 0.0d char '/ u0000' (expressed empty) Boolean False All Reference Type Null --------------------------- -------------------------------------------------- -------- Thus we can see the initial value of the AGE member variable in the object memory status diagram is 0. Once you have created a new object, you can use the "Object Name. Object member" format to access the object's member (including properties and methods), and demonstrate the generation and use of the Person class object: Class Person {Int Age; Void Shout () {system.out.println (age);}} class testperson {public static void main (string [] args) {person p1 = new person (); Person P2 = new person (); p1.age = - 30; p1.SHOUT (); p2.SHOUT ();}}} program operation results: -30 0 Analysis: Two Person class objects are created in the testperson.main method, and define the object reference handle of the Person class P1 , P2, points to both objects, respectively.
Next, the program calls the method and attributes of P1 and P2, P1, P2 are two fully independent objects, and the member variables defined in the class. They are individually instantiated in each object, and they will not be shared by all objects, change The AGE attribute of P1 does not affect the AGE attribute of P2. When calling an object, the member variable accessed inside the method is the member variable of this object itself, and the memory layout running on the above program is shown in Figure 2. Stack memory stack memory |. .............. | | ________ | |. | -----> | __- 30 _____________________________________ | - p1.SHOUT () {| _ of the access to AGE __ | ---- p2.SHOUT () {| _ one for Age access __ | ---- | | - | | - | ________ | -----> | ___ 0 ____ | AGE // P2 Object Figure 2 Each created object has its own lifecycle, the object can only be used in its valid lifecycle, when there is no reference variable When you point to an object, this object will become garbage and cannot be used anymore. The following is a specific garbage code and analysis: 1, the first case: {----- | Person p1 = new person (); | -> Person object is referenced; leaving the scope P1 failure, the Person object becomes garbage .