Java learning notes (initialization of member variables)

Source: Internet
Author: User
Tags java anonymous class

Class initialization includes instance Member initialization and static member initialization:
1. static member variable Initialization
Static variable initialization method:
A) assign public static string variablea = "hello" directly, // you can also call methods or other variables to assign values, but must meet the forward reference principle.
B) write the value assignment statement in the static clause: static {// initialization statement block}
Direct value assignment is triggered when the variable is referenced for the first time. Static clauses have to be triggered at two times. One is when the static variable is referenced, and the other is when the class is instantiated for the first time.
If both A and B are included, the initialization sequence is a-> B. this kind of initialization is a provision, just remember, of course, this rule industry must satisfy the rationality, this is the first time the class
The reason why static variables must be initialized before instantiation: The instance initialization may access static variables. Therefore, the static variable initialization policy of. C # Must be prepared, which is similar to that of Java.
The initialization clause is similar to the static constructor of C #. The initialization of static variables is not during class loading, but during class instantiation. class instantiation is triggered for the first time in the class variable
Static variables) When called, or when the object is instantiated for the first time.
2. instance Member Initialization
There are three methods for instance Member initialization:
A) Direct Value assignment: Public String variablea = "hello ";
B) In the instance initialization clause
Public Class {
// Instance initialization clause.
{
// Write the initialization statement here.
}

}
C) In the constructor.
The initialization of instance member variables is performed at the time of the instance, and the execution sequence is a-> B-> C.

The following is the test code:

Public class classa {/** static member variable */public static string variablea = "direct initialization value"; public static string variablea1;/** instance variable */Public String variableb; // static initialization clause static {system. out. println ("variable A:" + variablea); variablea = "variblea is initialized in the static clause! "; System. out. println ("variable A:" + variablea);} // instance Member initialization clause {system. out. println ("variable B before initialization:" + variableb); variableb = "varibleb initialization! "; System. Out. println (" variable B after initialization: "+ variableb) ;}} The following is the call test code:
// The following three statements can cause the execution of static initialization clauses, but note that the static initialization clauses are only executed once // string Thea = classa. variblea; string theb = classa. variablea; // classa theCa = new classa ();
// The following statement does not cause static variable initialization. That is to say, Java does not initialize the static variable during class loading. String thenamestring = classa. Class. getname ();
However, class theC = Class. forname ("classa") causes the execution of static clauses.

It seems that the initialization clause of an instance is unnecessary and can be replaced by constructors. However, this mechanism is critical for anonymous classes,
Because anonymous classes cannot write constructors. The anonymous type in C # is much simpler than the Java Anonymous class. One possible reason is that C # has
Delegate type, while Java needs to use anonymous classes to conveniently implement functions similar to C # Delegate.
Clarifying the initialization mechanism of member variables can prevent the emergence of some bugs.

PS: class. forname ("classa") will cause class loading and static variable initialization. Actually, the class itself is instantiated. For classa. Class. getname (),
Calling classa. Class obviously requires loading the classa class, but does not trigger the execution of static clauses, indicating that the class itself is not instantiated. Please be clear about the whole mechanism.

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.