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 / December 2005

Tip: Looking for answers? Try searching our database.

I need a static constructor

Thread view: 
timasmith@hotmail.com - 06 Dec 2005 02:26 GMT
Hi,

Perhaps I have been drawn to the dark side but I really want to have:

<code>
public class Myclass {

   private static int myVar = 0;

   static Myclass {
       mVar = InitializeValue();
   }

   public static int getMyVar() {
       return myVar;
   }

   private static void InitializeValue() {
       myVar = needToAlwaysInitThisFirst();
   }
   ....
}
</code>

Since one might forget to initialize the class with Myclass myclass =
new Myclass();
I would have to check within every static method to see if
InitializeValue had executed and execute it if not.

Is there a better way to make use of the convenience of static
classes/methods but ensuring the class has been initialized (values
loaded etc) prior usage - within resorting to coding conventions or
bloated methods?

thanks

Tim
Thomas Hawtin - 06 Dec 2005 03:13 GMT
> Perhaps I have been drawn to the dark side but I really want to have:

But this isn't a C# newsgroup...

> public class Myclass {
>
>     private static int myVar = 0;
>
>     static Myclass {

That should read:

      static {

>         mVar = InitializeValue();
>     }

> Since one might forget to initialize the class with Myclass myclass =
> new Myclass();
> I would have to check within every static method to see if
> InitializeValue had executed and execute it if not.

Using any static method of class constitutes use of the class, even
without calling an instance constructor. The static initialiser will
therefore have run. If you used an instance initialiser instead of
static initialiser, then the code would run for every instance created.

If you really want to you can get hold of the Class object for a class
without initialisation using Class.forName(String,false,ClassLoader).

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

timasmith@hotmail.com - 06 Dec 2005 03:46 GMT
Jean-Francois Briere - 06 Dec 2005 03:36 GMT
You can do either:

public class Myclass {

   private static int myVar = InitializeValue();

   public static int getMyVar() {
       return myVar;
   }

   private static void InitializeValue() {
       myVar = needToAlwaysInitThisFirst();
   }
   ....
}

or:

public class Myclass {

   private static int myVar = 0;

   static {
       myVar = needToAlwaysInitThisFirst();
   }

   public static int getMyVar() {
       return myVar;
   }
   ....
}

Either way, myVar will be initialized prior to any use of MyClass.

Regards
timasmith@hotmail.com - 06 Dec 2005 03:46 GMT
mrandywarner@gmail.com - 06 Dec 2005 15:11 GMT
You could always declare a private constructor and have a static method
for getting an instance of the class.

public class MyClass {

private int myVar = 0;

private MyClass {
   myVar = initializeValue();
}

public MyClass getInstance() {
   return new MyClass();
}
}

or you could use a static init block if you want the variable to remain
static.

public class MyClass {
   private static int myVar = 0;

   static {
       mVar = initializeValue();
   }

   public MyClass() {

   }
}


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



©2008 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.