Java object creation process addendum, java object Addendum

Source: Internet
Author: User

Java object creation process addendum, java object Addendum

1. static modified items belong to this class and are shared by all instances of this class. Therefore, their initialization is prior to the initialization of instance objects.

2. There is no static constructor in Java, but there is a static code block. Who executes static code blocks and static member variable declaration statements in a class first?

   

A: According to the top-down sequence defined in the class, who executes the task first.

3. Who should execute non-static code blocks and non-static member variable declaration statements in the class first?

   

A: According to the top-down sequence defined in the class, who executes the task first.

4. A non-static member variable declaration statement containing a value assignment, such as int v = 4. In fact, the execution of this statement is divided into two steps. First, initialize the variable v when creating the object, and then v = 0; then assign a value to v in the constructor, that is, v = 4.

5. The value assignment statement and instance variable value initialization value assignment statement in non-static initialization blocks are executed before the value 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 will be extracted to the constructor after being processed by the compiler, and before the original statements in the constructor.

Public class JavaTest {// member variable int count = 20; // non-static initialization code block {count = 12; System. out. print ("occupied location");} // constructor public JavaTest () {System. out. println (count);} public JavaTest (String name) {System. out. println (count );}}

After compilation, it is equivalent:

Public class JavaTest {// member variable int count; // non-static initialization code block {System. out. print ("occupied location");} // constructor public JavaTest () {count = 20; // The original member variable is assigned a value of count = 12; // The original initialization code block is assigned to System. out. println (count);} public JavaTest (String name) {count = 20; // The original member variable value is count = 12; // The original initialization code block value is System. out. println (count );}}

6. The statement "the Java object is created by the constructor" is incorrect. The constructor is only responsible for initializing the instance variables of Java objects, as mentioned in the preceding three, four, and five points. Before executing the constructor code, the memory occupied by the instance object has been allocated, and these memories are all null by default-for basic type variables, the default value is 0, 0.0, or false. For referenced variables, the default value is Null. Let's take a look at the following code and analyze what the result is:

      
Class base {private int I = 2; public Base () {here is 1
        this.display();    }    public void display()    {        System.out.println(i);    }}class Derived extends base{    private int i = 22;    public Derived()    {        i = 222;    }    public void display()    {        System.out.println(i);    }}public class Test{    public static void main(String[] args)    {        new Derived();    }}
  

Question 1: How many "I" instance variables are there in the instance of the Derived object? Say there is only one student, please go to the front

Question 2: Who is the "this" in the red part of the code? Is it a base instance or a Derived instance? The answer is Derived.

Question 3: In the code, "this. display ();" indicates the display method in which class is called? The answer is Derived.

Question 4: After Test is executed, the output is 2? 22? 222? The answer is 0.

7. the compiler is different in processing member variables and methods. When a subclass and its parent class have a member variable with the same name, each has a member variable with the same name. When there is a method in the subclass that has the same signature as the parent class, actually, the subclass overrides this method in the parent class. At this time, the method that calls this name through the subclass instance will always be the subclass method, even if you forcibly convert the subclass to the parent class. This is a bit confusing. Let's look at it directly:

Class base {private int I = 2; public Base () {this. display ();} public void display () {System. out. println ("My parent class" + I);} class Derived extends base {private int I = 22; public Derived () {I = 222;} public void display () {System. out. println ("I Am a subclass" + I );}}

Scenario 1:

  

Base b=new Base()

  

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

B. display (): it must be the method for calling the parent class, because it is the instance of the parent class currently.

Scenario 2:

  

Derived d = new Derived ();

B. count: It must be 22. Because the current object is a subclass instance, The subclass itself has a member variable named "I". Why does it take the parent class?

B. display (): subclass Method

Case 3:

  

Base bd = new Derived ();

B. count: It must be 2. Because the current subclass is converted up, you have to take the "I" of the parent class"

B. display (): it is still a subclass method. The upward conversion cannot be too much. The method that has been rewritten by the subclass cannot be repented. To call a method with the same name as the parent class, you must use this method: super. display ()

Case 4:

  

Derived d = new Derived ();

Base d2b = d;

B. count: It must be 2. Because the current subclass is forced to convert the type, you have to take the "I" of the parent class"

B. display (): it is also a subclass method. It cannot be too much to force type conversion, and the method that the subclass has already rewritten cannot be repented. To call a method with the same name as the parent class, you must use this method: super. display ()

8. From the perspective of memory allocation, when a program creates a subclass object, JVM not only allocates memory for the instance variables defined in this class, it also allocates memory for all instance variables defined in its parent class, even if the subclass defines instance variables 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 variable in the subclass hides the variable defined in the parent class, but is not completely overwritten, but cannot be used directly; when you convert a subclass to a parent class or use the "super" keyword, you can use the member variable with the same name in the parent class.


Java object creation process

It depends on whether you call constructor with or without parameters. When creating an object, the compiler will

Call the corresponding constructor of the parent class. If the class does not write the constructor, the compiler automatically adds

Default constructor without parameters.

Detailed steps for creating subclass objects in Java

1, 2 should execute static initialization code block;
It is basically correct. No problem.

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.