Initialization order of Java class member variables __java

Source: Internet
Author: User
The order of the   initialization is "static" (if they have not been initialized because of the previous object creation process), and then "Non-static". The specific creation process: 1. When a class object is first created, or when a static method/static field for that class is first accessed, the Java interpreter must find the path of the class to locate the class file for it. 2. Then load the class (Create a Class object), and the action on static initialization executes. Static initialization is performed only once when the class object is first loaded.   For example: perform initialization actions in order of occurrence static int n1 = f (1); static{   n3 = f (3);   //static domain initialization statements do not have to worry about illegal forward references, but if you are accessing N3, an illegal forward reference error is occurring    System.out.println ("println"); static int n2 = f (2); static int N3; static int f (int i) {   System.out.println ("n" +i);    return i} Create object or access a static domain/static method of this class, print out: N1 n3 PR Intln n2   3. When you create an object using the new operator, you first allocate enough storage space on the heap for the object you want to create. 4. This storage space will be cleared, which automatically sets all the base type data in the class to the default value (the value and character is 0, Boolean is false), and the reference is set to NULL. 5. Perform all initialization actions that occur at the domain (non-static domain) definition in the order in which they appear. 6. Execute builder.   Run the following example is clear.

public class Membersinitializesequence {

public static void Main (string[] args) {
BOWL.F ();
System.out.println ("Instantiation of a Bowl object");
New Bowl ();
System.out.println ("Re-instantiating a Bowl object");
New Bowl ();
}
}

Class Cup {
Cup (int i) {
System.out.println ("New Cup" ("+i+"));
}
}

Class bowl{
Cup C1;
cup C2 = new Cup (2);
{
C1 = new Cup (1);
}
static{
C4 = new Cup (4);
SYSTEM.OUT.PRINTLN ("Static fast in bowl, static block only run when class is loaded, so run only once");
}
Static cup C3 = New Cup (3);
Static Cup C4;
Bowl () {
System.out.println ("New Bowl ()");
}

static void F () {
System.out.println ("static method F () is accessed" in bowl);
}
}

In the case of a class being inherited, the order of initialization:

1. Initialize the subclass, the ClassLoader will find that it inherits another class when it loads it, and the class loader first loads the parent class in. The parent class is loaded and the static part of the parent class is loaded sequentially. The static portion of the subclass is then loaded. The order is to consider that the static domain of the subclass may depend on the static domain of the parent class;

2. The non-static part of the parent class is loaded;

3. The constructor of the parent class specified by the subclass;

4. The non-static parts of subclasses are loaded;

5. The remainder of the subclass builder.

Multiple levels of inheritance relationships, in the same order.

Cases:

public class Membersinitializesequenceex {
public static void Main (string[] args) {
New Cat ();
New Tiger ();
}
}
Class cat{
int C1 = f1 (1);
int F1 (int i) {
System.out.println ("Cat unstatic" +i);
return i;
}
{
System.out.println ("Cat unstatic block");
}
static int c2 = F2 (2);
static int F2 (int i) {
System.out.println ("Cat static" +i);
return i;
}
static{
System.out.println ("Cat static Block");
}
Public Cat () {
System.out.println ("New Cat ()");
}
}
Class Tiger extends cat{
int C3 = F1 (3);
@Override
int F1 (int i) {
System.out.println ("Tiger unstatic" +i);
return i;
}
{
System.out.println ("Tiger unstatic block");
}
static int c4 = f2 (4);
static int F2 (int i) {
System.out.println ("Tiger static" +i);
return i;
}
static{
System.out.println ("Tiger static Block");
}
Public Tiger () {
Super ();
System.out.println ("New Tiger ()");
}
}

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.