Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Who can tell me the initialzing sequence of a java programe?

Reply
Thread Tools

Who can tell me the initialzing sequence of a java programe?

 
 
Jack Dowson
Guest
Posts: n/a
 
      04-29-2007
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!
 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      04-29-2007
Jack Dowson <(E-Mail Removed)> writes:
>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/t...on.html#12.4.2

 
Reply With Quote
 
 
 
 
Jack Dowson
Guest
Posts: n/a
 
      04-29-2007
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!

 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      04-29-2007
Jack Dowson <(E-Mail Removed)> writes:
>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.

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      04-30-2007
Jack Dowson wrote:
> 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")?


The line b.print();

Non-static 'print()', called via the instance pointed to by 'b', which in turn
was created by 'new B()'. QED.

--
Lew
 
Reply With Quote
 
Jack Dowson
Guest
Posts: n/a
 
      04-30-2007
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!
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      04-30-2007
Jack Dowson wrote:
> 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.


--
Lew
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Some people tell that at present, most web hosting servers supportall kinds of programming language, some people tell me that many web hostingserver don't support Java, What is the truth? Erwin Moller Java 3 05-07-2008 05:09 PM
hey i am just started java,, can anyone tell me the use ,application, why java , importance of java.. manish sahu Java 3 02-14-2008 12:00 AM
how to iterate over sequence and non-sequence ? stef mientki Python 13 10-20-2007 10:21 AM
CAN any one tell it whats the code tell it yogesh C++ 1 03-14-2007 01:12 PM
BOOT SEQUENCE (how to change boot sequence) bird Computer Support 13 12-24-2003 02:20 AM



Advertisments