Java Basics Series 5: Java code execution order

This blog series will show you how to from entry to advanced, step by step to learn the basics of Java, and started actual combat, then understand Java knowledge to realize the principles behind each point, a more complete understanding of the entire Java technology system, to form their own intellectual framework.

 

First, the construction method

Constructor (or constructors) are a special class of the method for initialization of a new object class. Java Each class has a default constructor, and it must have the same name as the class name, and no return type. Default constructor return type is a type of the object itself, and construction methods can not be static, final, synchronized, abstract and native modification.

Tip: constructor to initialize a new object, it does not make sense with static modification; constructor can not be inherited by subclasses, so no modification with final and abstract sense; multiple threads does not create the same memory address of the same object at the same time, so no need to use synchronized modification.

Constructor syntax is as follows:

the Person class {public 
	
	/ ** 
	 * returns the value 1. The default constructor does not return type is the type of the object itself 
	 * 2. The same construction method name and method class name 
	 * / 
	
	// constructor with no arguments 
	public the Person () { 
		the System.out. println ( "I'm a no-argument constructor"); 
	} 
	
	// constructor parameters have 
	public the Person (String username, Integer Age) { 
		System.out.println ( "I have arg constructor" + "name:" + username + " password: "Age +); 
	} 
	
	public static void main (String [] args) { 
		the Person new new P1 = the Person (); // call the constructor with no arguments 
		
		Person p2 = new Person (" Wang ", 12); // call there configuration parameters 
	} 

}

  

On a constructor, you need to pay attention to:

  • How to call:
    • In the example of the constructor called when, as in the above code P1 Person = new Person (), where it is called Person class constructor without parameters, the system automatically calls the constructor
  • Overloaded constructor
    • We know that the method can override (the same method name, a different parameter list), then a method of constructing method is, of course, can be inherited as two constructors in the above code, a no reference constructor, with two constructor parameter.
    • When there are multiple constructors, the program will determine which configuration method is called when you create a class based on your parameters passed
  • The default constructor
    • Careful readers may be in doubt, create a class before when I did not declare a constructor, but you can also create a class, the class is not to say that does not require a constructor can also be created. Not a drop, when you declare the constructor does not appear, the program will automatically generate a default no-argument constructor
    • And permissions of the constructor is changed with the change in the class (class public, but also the constructor is public; class to private, Private constructors was changed) ; when once such a statement after the constructor, java is not going to give that class is assigned a default constructor. That is, once you declare a constructor, and the constructor physical parameters, then you can not pen ipen = new pen (); declares an object like this.
  • Constructor function:
    • The constructor for object initialization
    • An object is created, the constructor run only once, but the general method can be called multiple times the object.

 

Second, code block

1, the general block:

Common code block is our most used and most common, it is the method after the names enclosed in {} of code. Common code block that can not exist alone, it must immediately follow the method name. It must also be invoked using the method name.

the Test class {public 
    public void Test () { 
        System.out.println ( "normal block"); 
    } 
}

  

2, the configuration of the code block:

In the class defined directly without any modifiers, prefix, suffix code block is the code block structure. We understand that a class must have at least one constructor, the constructor is called when an object is generated. Configuration of code blocks and also as a constructor is called when an object is generated

the Test class {public 
  { 
      System.out.println ( "I is a structural block"); 
  } 
}    

 

note:

  • Tectonic code block is initialized to the subject.
  • Configured to establish a call to the object block, and it performs better than the constructor. Here emphasize that there are objects that are created, will execute the code block structure, class structure can not be called a block of code, structure and order of execution of the code block and constructor is the first implementation of the former than the latter.
  • The difference between code blocks and construction constructor is: a unified construction code block is initialized to all objects, and constructors are initialized to the corresponding object, because the constructor is more, which runs the constructor will establish what kind of object, but no matter which object is created, it will first perform the same configuration code block. That is, the code block structure is defined in the initialization of the contents of different objects in common.

  

 

3, the static code block:

Think we will think of static static, static code blocks is to use a modified static enclosed by {} code segments, its main purpose is to initialize the static attributes.

the Test class {public 
    static { 
        System.out.println ( "static block"); 
    } 
}

  

note:

  • Static block of code is performed with loading the class, but only once, and is superior to the main function. Specifically static block of code called by the class code block to perform a static class when invoked, and then performs the main function.
  • Static class initialization code block is given, the code block is constructed to initialize the object.
  • Static code block variables are local variables, and local variables in the conventional method is no difference.
  • A class can have a plurality of static code blocks.

 

Third, the Java class initialization sequence

1, a class situation:

A:

the Test class {public 
	
	public the Test () { 
		System.out.println ( "the Test constructor"); 
	} 
	
	{ 
		System.out.println ( "the Test block configured"); 
	} 
	
	static { 
		System.out.println ( "static code block "); 
	} 
	
	
	public static void main (String [] args) { 
		
	} 

}

  

result:

Static code block

  

B:

the Test class {public 
	
	public the Test () { 
		System.out.println ( "the Test constructor"); 
	} 
	
	{ 
		System.out.println ( "the Test block configured"); 
	} 
	
	static { 
		System.out.println ( "static code block "); 
	} 
	
	
	public static void main (String [] args) { 
		the Test new new T = the Test (); // create an object 
		
	} 

}

  

This code is compared to the above code to create a multi-object code

result:

Static code block 
Test block configured 
Test constructor

  

C:

the Test class {public 
	
	public the Test () { 
		System.out.println ( "the Test constructor"); 
	} 
	
	{ 
		System.out.println ( "the Test block configured"); 
	} 
	
	static { 
		System.out.println ( "static code block "); 
	} 
	
	
	public static void main (String [] args) { 
		the Test new new T1 = the Test (); // create an object of 
		
		the Test new new T2 = the Test (); 
		
	} 

}

  

result:

Static code block 
Test block configured 
Test Constructor 
Test block configured 
Test constructor

  

Whereby the results can be seen: a static code block is executed only once at the time the class is loaded, the function and configuration constructed in each block will be executed once the object is created

 

For a class, it performs the following sequence:

  1. Performing static code block
  2. Performing code block structure
  3. Constructor is executed

For static variables, static initialization block, variables initialization block, constructor, which is the order of the initialization (static variables, static initialization block)> (variables initialization block)> constructor.

 

D:

the Test class {public 
	
	// static variable 
	public static String staticField = "static variables"; 
	
	// variables 
	public String field = "Variable"; 
	
	// static initializer block 
	static { 
		System.out.println (staticField); 
		System.out.println ( "static initialization block"); 
	} 
	
	{ 
		System.out.println (Field); 
		System.out.println ( "initialization block"); 
	} 
	
	// constructors 
	public the Test () { 
		System.out.println ( "constructor "); 
	} 
	
	public static void main (String [] args) { 
		the Test new new T = the Test (); 
	} 

}

  

result:

Static variable 
static initialization block 
variables 
initialization block 
constructor

  

2, the order of execution of code in succession:

TestA {class 
	public TestA () { 
		System.out.println ( "constructor function A"); 
	} 
	
	{ 
		System.out.println ( "A configuration of the code block"); 
	} 
	
	static { 
		System.out.println ( "A static code block "); 
	} 
} 

public class TestB, the extends TestA { 
	
	public TestB, () { 
		System.out.println (" constructor of B "); 
	} 
	
	{ 
		System.out.println (" B configuration code blocks ") ; 
	} 
	
	static { 
		System.out.println ( "static code block B"); 
	} 

	public static void main (String [] args) { 
		TestB, new new TestB, T = (); 
	} 
	
}

  

There are two classes, relationships belong to the inheritance, readers do not read the answer, think for themselves about what is the result?

1  static code block A
 2  static code block B
 3  is configured to block A
 4  constructor A is
 5  configured to block B
 6 constructor of B
result

 

 

When designing the succession, the order of execution of the code as follows:

1, static code block performs the parent class, and initialize the static member of the parent class

2, execution subclass static block of code, and initialize static member subclass

3, the configuration of the block of code parent, the parent class constructor is executed, and initializes the common parent class member variables

4, the configuration of the block of code subclass, the subclass constructor performed, and initializes the common member variables subclass

 

Java initialization flow chart:

 

 

 

the Parent {class 
	/ * static variables * / 
	public static String = p_StaticField "parent - the static variable"; 
	/ * Variables * / 
	public String = p_Field "parent - variable"; 
	protected int = I. 9; 
	protected int J = 0; 
	/ * static initialization block * / 
	static { 
		System.out.println (p_StaticField); 
		System.out.println ( "parent - static initialization block"); 
	} 
	/ * initialize block * / 
	{ 
		System.out.println (p_Field); 
		System.out.println ( "parent - initialization block"); 
	} 

	/ * constructor * / 
	public the parent () { 
		System.out.println ( "parent - constructor"); 
		the System. Out.println ( "I =" + I + ", J =" + J); 
		J = 20 is;
	} 
} 

Public class SubClass the extends the Parent { 
	/ * static variables * /
	public static String s_StaticField = "subclass - static variables"; 
	/ * Variables * / 
	public String s_Field = "subclass - variable"; 
	/ * static initialization block * / 
	static { 
		System.out.println (s_StaticField); 
		the System .out.println ( "subclass - static initialization block"); 
	} 
	/ * initialize block * / 
	{ 
		System.out.println (s_Field); 
		System.out.println ( "subclass - initialization block"); 
	} 

	/ * constructor * / 
	public subClass () { 
		System.out.println ( "subclass - constructor"); 
		System.out.println ( "I =" + I + ", J =" + J); 
	} 

	/ * program entry * / 
	public static void main (String [] args) { 
		System.out.println ( "subclass main method"); 
		new new subClass (); 
	} 
}

  

result:

Parent - Static variable 
parent class - static initialization block 
subclass - Static variable 
subclass - static initialization block 
subclass main method 
parent - Variable 
parent class - initialization block 
parent class - constructor 
i = 9 , j = 0 
subclass - variable 
subclass - initialization block 
subclass - constructor 
i = 9, j = 20

  

(1) Access SubClass.main (), (this is a static method), so the loader will look for SubClass class code (ie SubClass.class file) has been compiled for you. In the loading process, it is noted that the loader has a base class (i.e. to be represented by means extends), so that reloading base class. Whether you create do not create a base class object, this process will always occur. If the base class has a base class, then the second base class will be loaded, and so on.

(2) the implementation of static initialization class roots, then the static initialization next derived class, and so on. This order is important because the derived class "static initialization" may have to rely on proper initialization of the base class members.

(3) When all the necessary classes have been loaded end, started main () method body, and create an object with new SubClass ().

There is a parent class (4) classes SubClass, then call the constructor of the parent class, you can use super to specify which constructor call. Base class construction process and construction order, the same as the derived class. First, each of the base class literally sequence variables are initialized, and then through the rest of the base class constructor.

(5) the subclass data members declared in the order of their initialization, performs the rest of the sub-class constructor. 

 

Guess you like

Origin www.cnblogs.com/wgblog-code/p/11849266.html