Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2008

Tip: Looking for answers? Try searching our database.

When gets the static initializer of a private static class exectuted?

Thread view: 
joes - 09 Jan 2008 07:38 GMT
Hello Gurus

I have a question. I have a class which holds an inner class declared
"private static". This inner class has a static field which is
immediately initilaized. When gets this static initilaziation of a
private static class exectuted?

- does it get executed when the outer class is loaded?
- does it get only executed if the inner class is getting loaded?

Example:
------------
public class Log {

   private static class LoggerHolder {
       public static Logger sLogger =
LoggerHelper.getLogger(LOGGER_CLASS);
   }

   public static Logger getLogger() {
       return LoggerHolder.sLogger;
   }
}

many thanks
regards

Mark
Mike Schilling - 09 Jan 2008 09:49 GMT
> Hello Gurus
>
> I have a question. I have a class which holds an inner class
> declared
> "private static". This inner class

Nitpick.  It's not an inner class.  Inner classes may not have static
fields.  It's a "nested" class.

> has a static field which is
> immediately initilaized. When gets this static initilaziation of a
> private static class exectuted?
>
> - does it get executed when the outer class is loaded?
> - does it get only executed if the inner class is getting loaded?

Only when the class which defines the static field gets loaded.
Lew - 09 Jan 2008 14:56 GMT
joes wrote:
>> I have a question. I have a class which holds an inner class
>> declared
>> "private static". This inner class

> Nitpick.  It's not an inner class.  Inner classes may not have static
> fields.  It's a "nested" class.

>> has a static field which is
>> immediately initilaized. When gets this static initilaziation of a
>> private static class exectuted?
>>
>> - does it get executed when the outer class is loaded?
>> - does it get only executed if the inner class is getting loaded?

> Only when the class which defines the static field gets loaded.

Only when the first object of the nested class type is used.  This is the
basis for a thread-safe lazy-loading idiom much ballyhooed by Brian Goetz, et al.
<http://www-128.ibm.com/developerworks/java/library/j-jtp03304/>
Within the outer class:
> private static class LazySomethingHolder {
>   public static Something something = new Something();
[quoted text clipped - 5 lines]
>   return LazySomethingHolder.something;
> }

The nested class's static initializer is not run when the outer class is
loaded but when the getInstance() method is first called.

Signature

Lew

Roedy Green - 09 Jan 2008 10:15 GMT
>- does it get executed when the outer class is loaded?
>- does it get only executed if the inner class is getting loaded?

Insert System.out.println( "inner(outer) static loading")
and tell us what you discover.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.