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 strong>
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. Strong>
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: strong>
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. Strong>
when creating an object, it will rigorously enforce object initialization: strong>
the first to call the parent class constructor the function. Strong>
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. Strong>
last rigorously enforce immediate constructor. Strong>
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. Strong>
summarize: strong>
1, class initialization strong>
(1) confession-style static initialization, static initialization block strong>
2, object initialization strong>
(1) the parent class constructor strong>
(2) confession-style initialization, initialization block strong>
(3) immediate constructor strong>
beware: strong> dr.dragongatemall.com strong> initial type of confession may not be accurate term, but find out what is on the line. Strong>
test code: strong>
code strong>
package principle; strong>
public class InitializeTest { strong>
public static void main (String [] args) throws ClassNotFoundException {< / strong>
System.out.println (“—– class initialization —–”); strong>
Class.forName (“principle.MyClass”); strong>
System.out.println (“/ n” “—– object initialization —–”); strong>
MyClass myClass = new MyClass (); strong>
} strong >
} strong>
class MyClass extends Father { strong>
MyClass ( ) { strong>
System.out.println (“3. personal constructor”); strong>
} < / strong>
static int staticInt = 5; nig.xhnight.com / / static {} if placed behind the compiler does not pass through strong>
static { strong>
System.out.print (“1. static initialization confession-style: staticInt =” staticInt “/ t”); strong>
System.out.println (“static initialization block”); strong>
} strong>
int instanceInt; / / will be assigned the default value 0 strong>
{ strong>
< strong> System.out.print (“2. initialized confession-style: instanceInt =” instanceInt “/ t”); strong>
System.out.println ( “initialization block”); strong>
} strong>
} strong>
class Father { strong>
Father () { strong>
System.out.println (“1 . the parent class constructor “); strong>
} strong>
} strong>
< br /> output gain: strong>
—– class initialization —– strong>
1. static initialization confession-style: staticInt = 5 static initialization block strong>
—– —– object initialized < / strong>
1. the parent class constructor strong>
2. initialized confession-style: instanceInt = 0 initialization block strong>
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. Strong>
JBoss Enterprise SOA Platform 4.3 and JBoss Operations Network 2.1 released strong>
ECLIPSE plug-in development was the PERMGEN SPACE disorders strong>
December 26, 2011
Java class initialization sequence
Comments Off
No Comments
No comments yet.
RSS feed for comments on this post.
Sorry, the comment form is closed at this time.