aspose file tools*
The moose likes Java in General and the fly likes static member initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "static member initialization" Watch "static member initialization" New topic
Author

static member initialization

Martin Lira
Ranch Hand

Joined: May 26, 2004
Posts: 97
Hi,
Can somebody explain difference between methods to initilize static fields of a class. When is the order of initialization important and which method to prefer if class A is a Java Bean to be invoked from a JSP
class A{
public static String HELLO = �HELLO WORLD�; //declare and initialize
}
class A{
public static String HELLO;
public A(){
HELLO = "HELLO WORLD"; //initialize
}
}
class A{
public static String HELLO;
static{
HELLO = �HELLO WORLD�;
}
}


Thanks,
ML
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Your first and third methods are equivalent. The middle one, though, is different and potentially bad, as the static variables will remain uninitialized (i.e., null/0/false) until an object of the class is created.

I generally use option 1; I use option 3 if I am forced to because a constructor needs to be in a try-block.


[Jess in Action][AskingGoodQuestions]
Dan Pollitt
Greenhorn

Joined: Jun 30, 2004
Posts: 9
The middle option would also re-initialize the static variable every time an instance was created, not exactly a massive performance hit at the moment but if this was more work it might be.

Might it leave the static class member in an inconsistent state in a multi-threaded environment, if it was a more complex piece of initialization?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Dan Pollitt:
Might it leave the static class member in an inconsistent state in a multi-threaded environment, if it was a more complex piece of initialization?


The second option, using the constructor, can leave the system in an inconsistent state *even in this simple example*. That is, other threads might still see the variable being not or only partly initialized.

Options 1 and 3 are both guaranteed to be thread safe, as far as I know.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608


Compiled into a static <clinit>.



Just plain wrong.
Also, referencing static members non-statically is fugly.



The same as the first one.

Order is important - initializers are executed in the order that they appear. This is in contrast to C# where order of static initialization is not important. For example, in Java, you will receive a compile-time error for a forward reference to a compile-time constant.


Tony Morris
Java Q&A (FAQ, Trivia)
Robert Konigsberg
Ranch Hand

Joined: Jun 23, 2004
Posts: 172
Question: Why would you want to *do* #2? Also, why would you NOT want HELLO to be final? If it's not final, then what, specifically, are you attempting to do?


SCJP 1.4 (91%)<br />SCJD 1.4 (376/400, 94%)
Martin Lira
Ranch Hand

Joined: May 26, 2004
Posts: 97
I agree, theres no problem in the static variable being final.
class A{
public static final String HELLO = �HELLO WORLD� //declare and initialize
}

//ML
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static member initialization