J
Jack Dowson
Hello,Everybody:
I'm new to java.I'm now confused by the sequence of initializing
static members of a class.
Here is an example which I wrote for testing:
class InitSequenceClassA{
static int i=0;
public InitSequenceClassA(){
i=15;
}
public InitSequenceClassA(int i){
this.i=i;
}
static void increment(){
i++;
}
}
class InitSequenceDemo{
public static void prt(String s){
System.out.println(s);
}
InitSequenceClassA isc = new InitSequenceClassA(10);
static InitSequenceClassA isc1, isc2;
static{
prt("isc.i=" + isc1.i+ "isc2.i=" + isc2.i);
isc1 = new InitSequenceClassA(27);
prt("isc1.i=" + isc1.i + "isc2.i=" + isc2.i);
isc2 = new InitSequenceClassA(15);
prt("isc1.i=" + isc1.i + "isc2.i=" +isc2.i);
}
public static void main(String[] args){
InitSequenceDemo d= new InitSequenceDemo();
prt("d.i=" + d.isc.i);
prt("isc.i=" + isc1.i+ " isc2.i=" + isc2.i);
isc1.increment();
prt("isc.i=" + isc1.i+ " isc2.i=" + isc2.i);
prt("d.i=" + d.isc.i);
}
}
Compile and excute this class we will get output as follow:
isc.i=0isc2.i=0
isc1.i=27isc2.i=27
isc1.i=15isc2.i=15
d.i=10
isc.i=10 isc2.i=10
isc.i=11 isc2.i=11
d.i=11
The first three lines of the output,that is
isc.i=0isc2.i=0
isc1.i=27isc2.i=27
isc1.i=15isc2.i=15
What happend to create the first three lines?
I think it might be:
1.the loading of class InitSequenceDemo;
2.after the loading of method main,the creating of object d;
which is right?
And which of the above actions comes first?
I mean loading of a class where main method located and loading of
method main,which comes first?
Any help will greatly be appreciated!
Thanks in advance!
I'm new to java.I'm now confused by the sequence of initializing
static members of a class.
Here is an example which I wrote for testing:
class InitSequenceClassA{
static int i=0;
public InitSequenceClassA(){
i=15;
}
public InitSequenceClassA(int i){
this.i=i;
}
static void increment(){
i++;
}
}
class InitSequenceDemo{
public static void prt(String s){
System.out.println(s);
}
InitSequenceClassA isc = new InitSequenceClassA(10);
static InitSequenceClassA isc1, isc2;
static{
prt("isc.i=" + isc1.i+ "isc2.i=" + isc2.i);
isc1 = new InitSequenceClassA(27);
prt("isc1.i=" + isc1.i + "isc2.i=" + isc2.i);
isc2 = new InitSequenceClassA(15);
prt("isc1.i=" + isc1.i + "isc2.i=" +isc2.i);
}
public static void main(String[] args){
InitSequenceDemo d= new InitSequenceDemo();
prt("d.i=" + d.isc.i);
prt("isc.i=" + isc1.i+ " isc2.i=" + isc2.i);
isc1.increment();
prt("isc.i=" + isc1.i+ " isc2.i=" + isc2.i);
prt("d.i=" + d.isc.i);
}
}
Compile and excute this class we will get output as follow:
isc.i=0isc2.i=0
isc1.i=27isc2.i=27
isc1.i=15isc2.i=15
d.i=10
isc.i=10 isc2.i=10
isc.i=11 isc2.i=11
d.i=11
The first three lines of the output,that is
isc.i=0isc2.i=0
isc1.i=27isc2.i=27
isc1.i=15isc2.i=15
What happend to create the first three lines?
I think it might be:
1.the loading of class InitSequenceDemo;
2.after the loading of method main,the creating of object d;
which is right?
And which of the above actions comes first?
I mean loading of a class where main method located and loading of
method main,which comes first?
Any help will greatly be appreciated!
Thanks in advance!