I need a static constructor

T

timasmith

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
 
T

Thomas Hawtin

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
 
J

Jean-Francois Briere

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
 
M

mrandywarner

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() {

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top