JAVA basics: initialization sequence of Constructor

Source: Internet
Author: User
JAVA basics: the initialization sequence of constructor methods-Linux general technology-Linux programming and kernel information. For details, refer to the following section. Imagine that you are writing A program in java and initializing objects like A and B with the following code:

Class {

Int a = f ();

Int f (){

Return 1;

}

}

Class B extends {

Int B =;

Int f (){

Return 2;

}

}

Public class CtorDemo1 {

Public static void main (String args []) {

B bobj = new B ();

System. out. println (bobj. B );

}

}



Now, it seems obvious that after Initialization is complete, the value of bobj. B will be 1. After all, the value of B in Class B is initialized with the value of A in Class a, while a is initialized with the value of f, and its value is 1, right?

In fact, the value of bobj. B is 2. You need to know why you need to know the object initialization problem.

When an object is created, Initialization is completed in the following order:

1. Set the member value to the default initial value (0, false, null)

2. Call the object constructor (but the constructor has not been executed)

3. Call the constructor of the parent class

4. Use the initialization program and the initial block to initialize members.

5. Execute the constructor



Let's take a look at how the process is completed step by step. Let's take a look at the example below:

Class {

A (){

System. out. println ("A. A called ");

}

}



Class B extends {

Int I = f ();

Int j;



{

J = 37;

System. out. println ("initialization block executed ");

}



B (){

System. out. println ("B. B called ");

}



Int f (){

System. out. println ("B. f called ");

Return 47;

}

}



Public class CtorDemo2 {

Public static void main (String args []) {

B bobj = new B ();

}

}



The program output is:



A. A called

B. f called

Initialization block executed

B. B called



B's constructor is called, but the first thing to do is to implicitly call the constructor of the parent class. The parent class must initialize its own State rather than the subclass.

Then, the member of object B is initialized, which includes a call to B. f and execution of the initial block enclosed in. Finally, the constructor of B is executed.

You may ask "What is an implicit call to the constructor of the parent class ". This means that if the first line of your constructor is not one of the following:



Super ();



Super (args );



This ();



This (args );



There are the following calls:



Super ();



The first line provided to the constructor.



What if the class has no constructor? In this case, a default constructor (also called "No-argument constructor") is automatically generated by the java compiler. The default constructor is generated only when the class does not have any other constructor.

To better understand this, let's assume there is such code in file A. java:



Public class {

Public static void main (String args []) {

A aref = new ();

}

}



If you want to compile and list the bytecode in A. class, enter the following content:



$ Javac A. java

$ Javap-c-classpath.



Output:



Compiled from A. java

Public class A extends java. lang. Object {

Public ();

Public static void main (java. lang. String []);

}



Method ()

0 aload_0

1 invokespecial #1

4 return



Method void main (java. lang. String [])

0 new #2

3 dup

4 invokespecial #3

7 astore_1

8 return

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.