Hands-on brain, self-confidence, achievement life

Source: Internet
Author: User

hands on the brain confidence and success in life

? hands on one's head

what is the result of the following code output? Please summarize the rules of Java field initialization according to the output of the code . and run the Teststaticinitializeblock.java example, observe the output, and summarize the "Order of execution of static initialization blocks".

GUESS: 200 300

Correct results: 100 300

Explain:

Java is initialized in two places: initialization blocks and constructors, where initialization blocks are divided into static initialization blocks and instance initialization blocks. Static initialization blocks are initialization blocks in a class that are decorated with static, and instance initialization blocks are initialization statements that do not have any keyword adornments in the class. The program must first reprint the class file into memory before defining the object of the class, and the class file is compiled, including the definition of the initialization process of the class, that is, the Java compiler collects all class variables (static variable) initialization statements and static initializers of the type < Within the Clinit> method, the method can only be called by the JVM and is specifically responsible for initialization. The class file corresponds to the member variable in the class with the "<init> ()" method. The compiler generates a "<init> ()" method for each constructor.

The static variable of a class is the class variable that is initialized after the first load, and this process is done only once. The initialization of a member variable of a class occurs when the object of the class is created after the mount is completed. It can be concluded that the initialization order of a class is a static member and then a non-static member. It should be noted that, whether it is a static member or a non-static member of the initialization, if there is a parent class is first to initialize the parent class, and then the child class initialization. Initialization blocks are equivalent to the initialization of a definition, and the order of the two is related to the precedence that occurs in the class. The constructor is entered when the initialization block is followed by the definition when initialization is complete.

Member variables of the same initialization order (that is, static variables and static initialization blocks or non-static) can only be initialized in the initialization block and cannot be referenced!

1. Execute the default value specified when the class member is defined or the initialization block of the class, exactly which one you want to see in front of.

2, the constructor of the execution class.

3, class initialization block does not receive any parameters, and as long as the creation of the object of the class, they will be executed. Therefore, it is appropriate to encapsulate those "code that must be executed when the object is created."

4. When there is an inheritance relationship between multiple classes, creating a subclass object causes the parent class to initialize the execution of the block.

In summary, the total load and initialization order of a class is: Static variable--static initialization block--static method--non-static variable--non-static initialization block--Non-static method--constructor.

Please write your own sample code to verify the above conclusions.

class Parent {

Public Static String P_staticfield = "Parent class-static variable";

Public String P_field = "Parent class-variable";

Static {

System. out. println (P_staticfield);

System. out. println ("Parent class-static initialization block");

}

{

System. out. println (P_field);

System. out. println ("Parent class-Initialization block");

}

Public Parent () {

System. out. println ("Parent class-constructor");

}

}

Public class Subclass extends Parent {

Public Static String S_staticfield = "sub-class-Static variable";

Public String S_field = "Subclass--variable";

Static initialization blocks

Static {

System. out. println (S_staticfield);

System. out. println ("subclass-static initialization block");

}

{

System. out. println (S_field);

System. out. println ("Subclass-Initialization block");

}

Public Subclass () {

System. out. println ("subclass--constructor");

}

Public Static void Main (string[] args) {

New Parent ();

System. out. println ("-------------------");

New Subclass ();

System. out. println ("-------------------");

New Subclass ();

}

}

Operation Result:

Parent class--Static variable

Parent class--static initialization block

Subclass--Static variables

Subclass--Static initialization block

Parent class--variable

Parent class--initialization block

Parent class--constructor

-------------------

Parent class--variable

Parent class--initialization block

Parent class--constructor

Subclass--Variables

Subclass--Initialization block

Sub-class--constructor

-------------------

Parent class--variable

Parent class--initialization block

Parent class--constructor

Subclass--Variables

Subclass--Initialization block

Sub-class--constructor

? hands on the brain two

Static methods only allow access to static data, so how to access the instance members of a class in a static method (that is, there is no additional static field or method of the keyword)?

Please write code to verify your idea.

A static method or property in a class is not a member of the class in nature, and when the Java Virtual machine is loaded in the class, the static objects already have the object, which is simply "resident" in the class and does not need to be instantiated by the class's constructor (constructor) class, rather than by static properties or methods. When a class is loaded, it does not exist, and it is necessary to have an instance object of the class that is dependent upon the constructor of the class being executed. So when a non-static method is called in a static method, the compiler will error. (Don't understand ...) )

public class a{

Class A non-static method

public void Func () {...}

Static methods in Class A (main function)

public static void Main (string[] args) {

A a=new a ();//A non-static method can be called after the object of a is instantiated

A.func ();

}

? hands on the brain three

Using the class's static fields and constructors, we can track the number of objects created by a class. Write a class that can query it at any time "how many objects have you created?" ”。

class jishu{

Private Static int a;

Public Jishu ()

{

a+ +;

}

Public Static int get ()

{

return a;

}

}

Public class Duixiangnum {

Public Static void Main (string[] args) {

Jishu J1=new Jishu ();

Jishu J2=new Jishu ();

System. out. println ("You have created" +jishu. Get() + "objects");

}

}

? hands on the brain four

Two is exactly the same for integers, why an output true , an output of false ?

(Tip: Use JAVAP to analyze the generated class file, see it called the Interger class, and then open the JDK source file to view the source code, you can find the answer. )

(Can not understand)

Hands-on brain, self-confidence, achievement life

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.