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
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