enclosed code with static???  
Author Message
QQ





PostPosted: 2006-11-10 8:44:00 Top

java-programmer, enclosed code with static??? Another question please.

why use static to enclose a few lines of code???
e.g.
static {
...
...
}

Thanks
Steven

 
Nicholas Sherlock





PostPosted: 2006-11-10 8:48:00 Top

java-programmer >> enclosed code with static??? QQ wrote:
> Another question please.
>
> why use static to enclose a few lines of code???
> e.g.
> static {
> ....
> ....
> }

This is a static initializer. It is run once when the class is first
loaded, and is typically used for setting up the values of static fields.

Cheers,
Nicholas Sherlock

--
http://www.sherlocksoftware.org
 
www.pulpjava.com





PostPosted: 2006-11-10 8:54:00 Top

java-programmer >> enclosed code with static??? The static code block gets called the first time a class is loaded.

This addresses the age old question: when should I initialize my static
variables? In a constructor? Somewhere else?

Well, the answer is simple: in a static code block.

Cheers!

-Cameron McKenzie

Free Sun Certified Java Associate Mock Exams: www.scja.com
Free Java and J2EE Tutorials: www.mcnz.com

www.technicalfacilitation.com www.pulpjava.com www.examscam.com


QQ wrote:
> Another question please.
>
> why use static to enclose a few lines of code???
> e.g.
> static {
> ...
> ...
> }
>
> Thanks
> Steven

 
 
QQ





PostPosted: 2006-11-10 9:23:00 Top

java-programmer >> enclosed code with static??? Cool. thanks for your help. Steven
Nicholas Sherlock wrote:
> QQ wrote:
> > Another question please.
> >
> > why use static to enclose a few lines of code???
> > e.g.
> > static {
> > ....
> > ....
> > }
>
> This is a static initializer. It is run once when the class is first
> loaded, and is typically used for setting up the values of static fields.
>
> Cheers,
> Nicholas Sherlock
>
> --
> http://www.sherlocksoftware.org

 
 
QQ





PostPosted: 2006-11-10 9:24:00 Top

java-programmer >> enclosed code with static??? Thank you, your answer are quite helpful for both of my questions,
really appreciate it. Steven

www.pulpjava.com wrote:
> The static code block gets called the first time a class is loaded.
>
> This addresses the age old question: when should I initialize my static
> variables? In a constructor? Somewhere else?
>
> Well, the answer is simple: in a static code block.
>
> Cheers!
>
> -Cameron McKenzie
>
> Free Sun Certified Java Associate Mock Exams: www.scja.com
> Free Java and J2EE Tutorials: www.mcnz.com
>
> www.technicalfacilitation.com www.pulpjava.com www.examscam.com
>
>
> QQ wrote:
> > Another question please.
> >
> > why use static to enclose a few lines of code???
> > e.g.
> > static {
> > ...
> > ...
> > }
> >
> > Thanks
> > Steven