I need a static constructor

Discussion in 'Java' started by timasmith@hotmail.com, Dec 6, 2005.

  1. Guest

    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
    , Dec 6, 2005
    #1
    1. Advertising

  2. wrote:
    >
    > 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
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
    Thomas Hawtin, Dec 6, 2005
    #2
    1. Advertising

  3. 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
    Jean-Francois Briere, Dec 6, 2005
    #3
  4. Guest

    nice, thanks
    , Dec 6, 2005
    #4
  5. Guest

    thanks
    , Dec 6, 2005
    #5
  6. Guest

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

    }
    }
    , Dec 6, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Giulio
    Replies:
    9
    Views:
    1,028
    Patrick Kowalzick
    Jun 25, 2003
  2. tropos
    Replies:
    3
    Views:
    448
  3. Replies:
    4
    Views:
    4,110
    Alf P. Steinbach
    Mar 3, 2006
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,197
  5. o_ssie
    Replies:
    0
    Views:
    309
    o_ssie
    Apr 9, 2009
Loading...

Share This Page