Addendum to the Java object creation process

Source: Internet
Author: User

The static modified Dongdong belongs to this class and is shared by all instances of the class, so their initialization precedes the initialization of the instance object.

There is no static construction method in Java, but there is static code block. When static code blocks and static member variable declaration statements exist in a class, who executes them first?

A: As defined in the class, the top-to-bottom order, who is in front of who first executed.

Third, when non-static code blocks and non-static member variable declaration statements exist in the class, who executes them first?

A: As defined in the class, the top-to-bottom order, who is in front of who first executed.

Four, contains the assignment of non-static member variable declaration statement, such as int v=4, in fact, the execution of this statement is divided into two steps. When the object is first created, the variable v is initialized, and v=0 is then assigned to V in the constructor, which is v=4.

The assignment statement in the non-static initialization block, the instance variable value initialization assignment statement, are executed first than the assignment statement in the constructor.

The principle is as follows: whether it is an assignment statement in a non-static initialization block, or an instance variable value initialization assignment statement, they are extracted into the constructor after the compiler is processed, and before the statements in the constructor.

 Public class javatest{    // member variable    int  count=20;     // non-static initialization of code blocks     {        =N;        System.out.print ("occupy a position");    }     // Constructors     Public javatest ()    {        System.out.println (count);    }      Public javatest (String name)    {        System.out.println (count);    }}

After compiling is equivalent to:

 Public classjavatest{//member Variables    intcount; //non-static initialization of code blocks{System.out.print ("A great place to be."); }    //Constructors     PublicJavatest () {count= 20;//The original member variable is assigned a valuecount=12;//original initialization code block assignmentSystem.out.println (count); }     Publicjavatest (String name) {Count= 20;//The original member variable is assigned a valuecount=12;//original initialization code block assignmentSystem.out.println (count); }}

Six, "Java objects are created by the constructor" is a bad sentence. The constructor is only responsible for initializing the instance variables of the Java object, as mentioned in the previous three or 45 points. Before executing the constructor code, the memory of the instance object is already allocated, which defaults to null-the default null value is 0, 0.0, or FALSE for a variable of the base type, and the default control is null for variables of reference type. Take a look at the following code to analyze what the result is:

class base{    privateint i = 2;      Public Base ()    {        This is 1 places
         This. display (); }     Public voiddisplay () {System.out.println (i); }}classDerivedextendsbase{Private inti = 22;  PublicDerived () {i= 222; }     Public voiddisplay () {System.out.println (i); }} Public classtest{ Public Static voidMain (string[] args) {NewDerived (); }}

Question one: Are there several "I" instance variables in an instance of the derived object? Say that there is only one classmate, please go to wall yourself

Question two: The code in the Red 1, referring to the "This" is who?  Is it an instance of base or an instance of derived? The answer is Derived.

Question three: Red 1 in the code, "this. Display ();       "Which class is called the display method?" The answer is Derived .

Question four: Does the output after test be 2? 22?   Or is it 222? The answer is 0.

The compiler is different when dealing with member variables and methods. When a subclass and a parent class have a member variable of the same name, they actually have a member variable with the same names, and when there is a method in the subclass that has the same signature as the parent class, it is actually a subclass that overrides the method in the parent class, and the method that calls the name through an instance of the subclass will always be a subclass. Even if you cast the class into a parent class. That's a little confusing, just looking down:

classbase{Private inti = 2;  PublicBase () { This. display (); }     Public voiddisplay () {System.out.println ("I am the parent class" +i); }}classDerivedextendsbase{Private inti = 22;  PublicDerived () {i= 222; }     Public voiddisplay () {System.out.println ("I am a subclass" +i); }}

Situation One:

Base B=new Base ()

B.count: Must be 2 because the current object is an instance of the parent class

B.display (): Must be a method to call the parent class because it is currently an instance of the parent class

Situation Two:

Derived d=new Derived ();

B.count: It must be 22, because the current object is an instance of a subclass, and the subclass has a member variable called "I". Why take the parent class's

B.display (): Methods of subclasses

Situation Three:

Base bd=new Derived ();

B.count: It must be 2, because the current subclass is being converted upward, having to take the parent class "I"

B.display (): Or the method of the subclass, the upward conversion can not be too much, the subclass has been overridden by the method can no longer regret. To invoke a method of the same name as the parent class, this is the way: Super.display ()

Situation Four:

Derived d=new Derived ();

Base D2b=d;

B.count: It must be 2, because the current subclass is coerced and has to take the parent class "I"

B.display (): Or a subclass of the method, coercion type conversion is not too much, the subclass has been overridden by the method can no longer regret. To invoke a method of the same name as the parent class, this is the way: Super.display ()

Viii. from a memory allocation perspective, when a program creates a subclass object, the JVM allocates memory not only for the instance variables defined in the class, but also for all instance variables defined in its parent class, even if the child class defines an instance variable with the same name as the parent class. If a member variable with the same name as the parent class is defined in the subclass, the variables in the subclass will hide the variables defined in the parent class, but not completely, but not directly; when the handle class is converted to a parent class or the "super" keyword is used, the member variable of the same name in the parent class can be used.

Addendum to the Java object creation process

Related Article

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.