🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: cn.comp.lang.java
3 messages
3 total messages Started by Jack Dowson Wed, 25 Apr 2007 16:53
Help!Maybe it's very simple for you?
#49
Author: Jack Dowson
Date: Wed, 25 Apr 2007 16:53
73 lines
2656 bytes
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!
Re: Help!Maybe it's very simple for you?
#50
Author: Paul Heing
Date: Sat, 28 Apr 2007 11:44
120 lines
4689 bytes
Jack Dowson wrote:
> 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!

Hello.
    Here is my opinion, though I have little experience with java. (and
forgive my poor English)
    From the result you can see that the static method without name
                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);
 		}	
is loaded before method main, and static variables isc1 and isc2 are
loaded before the static method without name, because you can use them
in the method.
    If you change the code:
    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);
    }
to
    {
		prt("isc.i = " + isc.i);
		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);
    }
the result will be that:
isc.i = 10
isc.i=10isc2.i=10
isc1.i=27isc2.i=27
isc1.i=15isc2.i=15
d.i=15
isc.i=15 isc2.i=15
isc.i=16 isc2.i=16
d.i=16
    If you analysize the output you will get the sequence of loading:
static variables, static method without name, nonstatic variables(you
can use isc.i which is nonstatic in the changed code), nonstatic method
without name, the static main method.
    If I'm wrong at anywhere, please point it out. Thank you.
Re: Help!Maybe it's very simple for you?
#51
Author: Philip
Date: Sat, 16 Jun 2007 16:31
129 lines
4749 bytes
Hi jack,
It's an intersting question. I will try my best to explain it for you.

When u excuting the main method, the class InitSequenceDemo that contain the 
main method will be loaded first.
As this loading, all the static fileds and static methods will also be 
initialized for the first time. And the fileds and methods loaded sequence 
is the same as they are in the class, that means static InitSequenceClassA 
isc1 you wrote first, it will be loaded first, then isc1, and the static { 
prt("isc.i=" + isc1.i+ "isc2.i=" + isc2.i); ...... follows.
Now, let's try to explain the results as you see.
When isc1 was loaded, it is only get an refrence, notice this, there is not 
any InitSequenceClassA object was created!!
As now, InitSequenceClassA.i(equals with isc.i, because i is static) is 
nothing but default int value 0.
Do u understand this?
I means if no InitSequenceClassA object exits, ofcause no constractor 
method(public InitSequenceClassA()) is called! So, InitSequenceClassA.i must 
not be 15.

Let's go on.

look at these codes:
> static{
> prt("isc.i=" + isc1.i+ "isc2.i=" + isc2.i);
> isc1 = new InitSequenceClassA(27);
> prt("isc1.i=" + isc1.i + "isc2.i=" + isc2.i);

The 3rd line told us a really InitSequenceClassA object was created, and its 
refrence is isc1. The constractor(public InitSequenceClassA(int i) ) of 
InitSequenceClassA was called for the first time, so the i was 27.

The next line code was be excuted then, the second InitSequenceClassA object 
was created, and the i was 15. Because the filed i is staic, all the filed 
i'value was be asigned 15 no matter ojbect isc1 and isc2. And the results 
proved it.

> isc.i=0isc2.i=0
> isc1.i=27isc2.i=27
> isc1.i=15isc2.i=15


Hope my explaining will help you.

Philip






"Jack Dowson" <jckdwsn@aol.com> д����Ϣ����:f0o3gb$jou$1@news.cn99.com...
> 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!
>
>
>
> 
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads