Article View: cn.comp.lang.java
Article #51Re: Help!Maybe it's very simple for you?
From: Philip
Date: Sat, 16 Jun 2007 16:31
Date: Sat, 16 Jun 2007 16:31
129 lines
4749 bytes
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!
>
>
>
>
Technical Details
Message-ID:
<f516ba$24d$1@news.cn99.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!news.cn99.com!not-for-mailReferences:
<f0o3gb$jou$1@news.cn99.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10d5a2,d8a5adad2c9756f3 X-Google-Attributes: gid10d5a2,public,usenet X-Google-Language: ENGLISH,ASCII Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!news.cn99.com!not-for-mail From: "Philip" <ldyao@tom.com> Newsgroups: cn.comp.lang.java Subject: Re: Help!Maybe it's very simple for you? Date: Sun, 17 Jun 2007 00:31:16 +0800 Organization: SYNNEX Lines: 131 Message-ID: <f516ba$24d$1@news.cn99.com> References: <f0o3gb$jou$1@news.cn99.com> Reply-To: "Philip" <ldyao@tom.com> NNTP-Posting-Host: 222.212.46.142 X-Trace: news.cn99.com 1182014635 2189 222.212.46.142 (16 Jun 2007 17:23:55 GMT) X-Complaints-To: usenet@news.cn99.com NNTP-Posting-Date: Sat, 16 Jun 2007 17:23:55 +0000 (UTC) X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.3138 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3138