🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: cn.comp.lang.java
Article #50

Re: Help!Maybe it's very simple for you?

#50
From: 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.
Technical Details
Message-ID: <f0vbqt$blt$1@news.yaako.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx02.iad01.newshosting.com!newshosting.com!198.186.194.247.MISMATCH!news-out.readnews.com!news-xxxfer.readnews.com!panix!bloom-beacon.mit.edu!news.cn99.com!news.yaako.com!not-for-mail
References: <f0o3gb$jou$1@news.cn99.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10d5a2,d8a5adad2c9756f3
X-Google-Attributes: gid10d5a2,public
X-Google-Language: ENGLISH,ASCII-7-bit
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx02.iad01.newshosting.com!newshosting.com!198.186.194.247.MISMATCH!news-out.readnews.com!news-xxxfer.readnews.com!panix!bloom-beacon.mit.edu!news.cn99.com!news.yaako.com!not-for-mail
From: Paul Heing <paulheing@yahoo.com>
Newsgroups: cn.comp.lang.java
Subject: Re: Help!Maybe it's very simple for you?
Date: Sat, 28 Apr 2007 19:44:03 +0800
Organization: Bentium Ltd. (CN99)
Lines: 120
Message-ID: <f0vbqt$blt$1@news.yaako.com>
References: <f0o3gb$jou$1@news.cn99.com>
NNTP-Posting-Host: 124.249.176.62
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-Trace: news.yaako.com 1177760416 11965 124.249.176.62 (28 Apr 2007 11:40:16 GMT)
X-Complaints-To: usenet@news.yaako.com
NNTP-Posting-Date: Sat, 28 Apr 2007 11:40:16 +0000 (UTC)
User-Agent: Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.7.13) Gecko/20061027
X-Accept-Language: zh-cn, zh, en-us, en
In-Reply-To: <f0o3gb$jou$1@news.cn99.com>