Designer handbags from All Handbag Fashion

December 26, 2011

Java class initialization sequence

Filed under: Fashion — Tags: — admin @ 12:59 pm

July 30, 2011
important reminder: The system detects that your account may have stolen the risk, please see the risk warning as soon as possible, and immediately change your password. Close
Netease blog security alert: The system detected that your current password is less secure, for your account security, we recommend that you change your password immediately amend the timely closure
Java class initialization order
I put the Java class initialization sequence is divided into two part. The first part is the class initialization, the second part is the initialization of the object.
before the initialization of the JVM bytecode will certainly first class loaded into memory. Then regardless of record will not immediately its efforts to create an object class initialization:
Li Xingjing confession-style initialization state (such as static int a = 10) and the initial static of the block (ie, static {} in the content), be careful both in the source code according to the order in its efforts to.
when creating an object, it will rigorously enforce object initialization:
the first to call the parent class constructor the function.
confession-style initialization step with its efforts (such as int a = 10) and initialization block ({} in the content), the same source code according to the the order in which rigorously enforce.
last rigorously enforce immediate constructor.
confession-style initialization for static and initialized confession-style, if only statement has not been initialized, the default values. Such as int i; then i will be assigned to 0.
summarize:
1, class initialization
(1) confession-style static initialization, static initialization block
2, object initialization

(1) the parent class constructor
(2) confession-style initialization, initialization block
(3) immediate constructor
beware: dr.dragongatemall.com initial type of confession may not be accurate term, but find out what is on the line.
test code:
code
package principle;
public class InitializeTest {
public static void main (String [] args) throws ClassNotFoundException {< / strong>
System.out.println (“—– class initialization —–”);
Class.forName (“principle.MyClass”);
System.out.println (“/ n” “—– object initialization —–”);
MyClass myClass = new MyClass ();
}
}
class MyClass extends Father {
MyClass ( ) {
System.out.println (“3. personal constructor”);
} < / strong>
static int staticInt = 5; nig.xhnight.com / / static {} if placed behind the compiler does not pass through
static {
System.out.print (“1. static initialization confession-style: staticInt =” staticInt “/ t”);
System.out.println (“static initialization block”);
}
int instanceInt; / / will be assigned the default value 0
{
< strong> System.out.print (“2. initialized confession-style: instanceInt =” instanceInt “/ t”);
System.out.println ( “initialization block”);
}
}
class Father {
Father () {
System.out.println (“1 . the parent class constructor “);
}
}
< br /> output gain:
—– class initialization —–
1. static initialization confession-style: staticInt = 5 static initialization block
—– —– object initialized < / strong>
1. the parent class constructor
2. initialized confession-style: instanceInt = 0 initialization block
3. even if the constructor of the immediate decision to inquire about the issue enough to put forth the magnificent mix, and indeed received a degree will certainly win.
JBoss Enterprise SOA Platform 4.3 and JBoss Operations Network 2.1 released
ECLIPSE plug-in development was the PERMGEN SPACE disorders

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

www.ybhaiwai.cn www.cms52.cn