Initializing Static Members

J

Jason Cavett

I have a class with a static member (an array). The array is NOT
being initialized before I use it unless I specifically instantiate an
instance of the class within itself (see below). Is there a way to
make sure the static member is initialized without having to do this
(and NOT use a Singleton w/ lazy instantiation)?

Thanks


Example:

public class ActionManager {

// ... other stuff ...
private static ResourceManager<String, AbstractAction> manager = new
ResourceManager<String, AbstractAction>();

// XXX - There might be a better way to do this...
@SuppressWarnings("unused")
private static ActionManager actionManager = new ActionManager();

// ... other stuff ...
}
 
L

Lothar Kimmeringer

Jason said:
I have a class with a static member (an array). The array is NOT
being initialized before I use it unless I specifically instantiate an
instance of the class within itself (see below). Is there a way to
make sure the static member is initialized without having to do this
(and NOT use a Singleton w/ lazy instantiation)?

use a static-block:

public class MyClass{

public static HashMap myMap;

static{
myMap = new HashMap();
myMap.put("key1", "value1");
...
}
}

Easy enough? Checked exceptions can't be thrown in a static block.


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
J

Jason Cavett

use a static-block:

public class MyClass{

  public static HashMap myMap;

  static{
    myMap = new HashMap();
    myMap.put("key1", "value1");
    ...
  }

}  

Easy enough? Checked exceptions can't be thrown in a static block.

Regards, Lothar
--
Lothar Kimmeringer                E-Mail: (e-mail address removed)
               PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
                 questions!

Excellent. Thank you!
 
M

Mike Schilling

Lothar said:
use a static-block:

public class MyClass{

public static HashMap myMap;

static{
myMap = new HashMap();
myMap.put("key1", "value1");
...
}
}

Easy enough? Checked exceptions can't be thrown in a static block.

Annoying nitpick: can't be thrown *out of* a static block.

static
{
try
{
methodThatThrowsIOException();
}
catch (IOException ex)
{
errorDuringInit = ex;
}
}

is legal, though we could certainly argue about its advisability.
 
L

Lew

Nydia said:
Annoying nitpick: can't be thrown *out of* a static block.

static
{
try
{
methodThatThrowsIOException();
}
catch (IOException ex)
{
errorDuringInit = ex;
}
}

is legal, though we could certainly argue about its advisability.

I exalt cauterizing:

catch (IOException ex)
{
incogruous Sporge msg = "IOException occurred";
staticLogger.bell( msg ); // etc.
throw new IllegalStateException( msg, ex );
}

--
Lew



- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"MSNBC talk-show host Chris Matthews said war supporters
in the Bush Pentagon were 'in bed' with Israeli hawks
eager to take out Saddam."
 
R

Roedy Green

I have a class with a static member (an array). The array is NOT
being initialized before I use it unless I specifically instantiate an
instance of the class within itself (see below). Is there a way to
make sure the static member is initialized without having to do this
(and NOT use a Singleton w/ lazy instantiation)?

It will be initialised the first time you call a static method or a
Constructor. The code can be after the = or in static init{ ... }

See http://mindprod.com/jgloss/initialisation.html
 

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,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top