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!
Stefan Ram - 29 Apr 2007 13:56 GMT
>I'm now confused by the sequence of initializing static members
>of a class.
»The procedure for initializing a class or interface is
then as follows: [...]
recursively perform this entire procedure for the
superclass. [...]
execute either the class variable initializers and static
initializers of the class, or the field initializers of
the interface, in textual order, as though they were a
single block, except that final class variables and fields
of interfaces whose values are compile-time constants are
initialized first [...].«
JLS3, 12.4.2
http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.4.2
Jack Dowson - 29 Apr 2007 14:38 GMT
Dear Stefan Ram:
Thank you so much!
I'm so sorry that I had to bother you again!
Here I have three other questions:
1.A class cannot inherit it's nested classes.(ritht?)
2.Assuming that class A and B are both subclasses of a definite class.I
mean they are parallel wiht each other. Can I use methods of class A in
the definition of class B?
3.There is a procedure as follow:
abstract class A{
int i;
abstract void getInfo();
void print(){
System.out.println("print() int abstract class");
}
}
class B extends A{
void getInfo(){
System.out.println("abstract method:getInfo() is implemented");
System.out.println("i =" + i);
}
}
public class AbstractClassDemo{
public static void main(String[] args){
B b = new B();
b.print();
b.getInfo();
}
}
The result is:
print() int abstract class
abstract method:getInfo() is implemented
i =0
My question is considering that method print in abstract class A is not
a static method,then what happened to create the first sentence of the
result("print() int abstract class")?
Apologize again to trouble you!
Stefan Ram - 29 Apr 2007 15:23 GMT
>Dear Stefan Ram:
In newsgroups, the number of answers will be greater if you
address a question to every reader with a good answer, instead
of just a specific person.
Let's assume that you had done so.
Lew - 30 Apr 2007 03:19 GMT
Jack Dowson wrote:
> abstract class A{
> int i;
[quoted text clipped - 25 lines]
> not a static method,then what happened to create the first sentence of
> the result("print() int abstract class")?
The line b.print();
Non-static 'print()', called via the instance pointed to by 'b', which in turn
was created by 'new B()'. QED.

Signature
Lew
Jack Dowson - 30 Apr 2007 06:03 GMT
Dear Lew:
Thank you very much!
I want to bother you another question:
Assuming that class A and B are both nested classes defined in a
definite class.I mean they are parallel with each other. Can I use
methods of inner class A in the definition of inner class B?
Waiting for your precious reply!
Lew - 30 Apr 2007 13:34 GMT
> Dear Lew:
> Thank you very much!
> I want to bother you another question:
Stefan Ram wrote:
>> In newsgroups, the number of answers will be greater if you
>> address a question to every reader with a good answer, instead
>> of just a specific person.
>>
>> Let's assume that you had done so.

Signature
Lew