There are no inheritance relationships objects in various classes that do not exist object references are created in the order of initialization

Source: Internet
Author: User

Example 3

class One
{
One (String str)
{
System.out.println (str);
}
}
class A
{
One one_1 = new One ("one-1");
One one_2 = new One ("one-2");
One one_3 = new One ("one-3");
Both (String str)
{
System.out.println (str);
}
}
Public class Test
{
public static void Main (string[] args)
{
SYSTEM.OUT.PRINTLN ("Test main () Start");
The other is new ("both");
}
}

Output Result:
Test main () Start ...
One-1
One-2
One-3
Both
An object in the main () method that instantiates a class of one or both. However, when the program initializes the object of the class of both classes, it does not first invoke the constructor of the class of both, but instead initializes the member variables of the first class. Here the two classes have 3 member variables, all of which are objects of the one class, so you first call the corresponding construction method of the one class 3 times. Finally, the object that initializes the class of both.
That is, when you create an object , all the data members of the class that the object is in are initialized first, and if the member variables have objects, they also perform the initialization work in order. After all class member initialization is complete, the object is created by invoking the constructor of the class where the object is located. The constructor method is the initialization of the function.



Example 4:

class One

{
One (String str)
{
System.out.println (str);
}
}
class A
{
One one_1 = new One ("one-1");
One one_2 = new One ("one-2");
Static one one_3 = new One ("one-3");
Both (String str)
{
System.out.println (str);
}
}
Public class Test
{
public static void Main (string[] args)
{
SYSTEM.OUT.PRINTLN ("Test main () Start");
Two_1 = new ("Two-1");
System.out.println ("------------");
Two_2 = new ("Two-2");
}
}

Output Result:
Test main () Start ...
One-3
One-1
One-2
Two-1
------------
One-1
One-2
Two-2
If there is a static object in a class, it is initialized before the non-static object , but only once . Non-static objects are initialized each time they are called .

Example 5

class One
{
One (String str)
{
System.out.println (str);
}
}
class A
{
One one_1 = new One ("one-1");
One one_2 = new One ("one-2");
Static one one_3 = new One ("one-3");
Both (String str)
{
System.out.println (str);
}

}
Public class Test
{
Static Two_3 = new ("two-3");
public static void Main (string[] args)
{
SYSTEM.OUT.PRINTLN ("Test main () Start");
Two_1 = new ("Two-1");
System.out.println ("------------");
Two_2 = new ("Two-2");
}
}

Output Result:
One-3
One-1
One-2
Two-3
Test main () Start ...
One-1
One-2
Two-1
------------
One-1
One-2
Two-2
static variables/static objects in the main class in the program are initialized before the main () method executes. Only one one-3 is output in the result, which means that if there is a static object in a class, it is initialized before the non-static object, but only once. Non-static objects are initialized each time they are called.

Example 6

class One
{
One (String str)
{
System.out.println (str);
}
}
class A
{
static int i = 0;
One one_1 = new One ("one-1");
Static one one_2 = new One ("one-2");
Static one one_3 = new One ("one-3");
Both (String str)
{
System.out.println (str);
}
}
Public class Test
{
public static void Main (string[] args)
{
SYSTEM.OUT.PRINTLN ("Test main () Start");
System.out.println ("two.i =" two.i);
}
}

Output Result:
Test main () Start ...
One-2
One-3
TWO.I = 0
Description under the hierarchy of the same class:

When you create an object for the 1th time , all of the static variables in the class are initialized, and the static variables in the class are accessed for the 1th time (no objects are created) , and all the static variables in the class are initialized in the order in which they are arranged in the class.

Note: In General, in the main program you need to first create the object , and then go to access the object's variables or methods , in this process you need to follow the object initialization steps to complete the process , Once you're done, you'll be able to access specific variables or methods .
In special cases , when accessing class variables or class methods directly in the main program, because class variables and class methods are not dependent on the existence of objects, so before access, the main program is not required to create the object (new), then the program runs the process is: 1. The main program class

Summary initialization sequence: 1. The static members of the main class are initialized first.
2. The constructor method of the parent class of the main class is called.
3. Initializes the non-static object (variable) of the main class.
4. Invokes the construction method of the main class.


The order of initialization includes the order in which the method calls are constructed:

1. Static members (static objects or variables) of the main program class are initialized first.

2. The constructor of the superclass of the main class is invoked in the order from highest to lowest (superclass to main class), and non-static members (non-static objects or variables) that are initialized at the same time are instance variables.

. Constructor:
When a subclass inherits a parent class, the constructor of the parent class needs to be called when the subclass is constructed, there are three cases
(1), the parent class has no constructor or a parameterless constructor, if the subclass does not have a constructor or parameterless constructor, the subclass constructor does not need to explicitly call the parent class constructor, the system will automatically call the subclass constructor before calling the parent class constructor
(2), the parent class only has the parameter constructor, the subclass must explicitly call the parent class's constructor in the constructor method, or the compilation error
(3), the parent class has both a parameterless constructor and a parameter constructor, and the subclass can not call the constructor of the parent class in the constructor, and the parameterless constructor of the parent class is used

3. Invokes the construction method of the main class.

There are no inheritance relationships objects in various classes that do not exist object references are created in the order of initialization

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.