NEWBIE: Can any method be invoked at class-loading time?

Dear Experts:

I have a MyConstants class, which holds company name, etc.  Everything in it has been defined as public static final.  Now I'd like to add some assignment statements that may as well be static.  Something like initNames below:

public class MyConstants
{
  public static final String[] names;
  public static final int BOB_INDEX = 0;
  public static final int ALICE_INDEX = 2;

  public void initNames()
  {
    names[BOB_INDEX] = "Bob";
    names[ALICE_INDEX] = "Alice";
  }
}
But currently, I never actually instantiate the MyConstants class.  Therefore, even if I put these new assignment statements in the MyConstants class constructor, it does no good--because it never get's constructed.

I have a vague recollection of how to solve this--something about static methods called at class-load time.  Am I making that up?  Is there a way to have this method run (once) without explicitly calling it somewhere?

Thanks!
--BrianMc1958
BrianMc1958Asked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
You could do

static
  {
    names[BOB_INDEX] = "Bob";
    names[ALICE_INDEX] = "Alice";
  }
0
 
sciuriwareConnect With a Mentor Commented:
You can put those assignments in a static block:

static
{
    names[BOB_INDEX] = "Bob";
    names[ALICE_INDEX] = "Alice";
}

that is executed at load time (in effect, at reference time).

Another approach would be:

public static final String[] names = new String[]{"Bob", "someone", "Alice");

;JOOP!
0
 
sciuriwareCommented:
Sorry, answered in parallel ...................................

;JOOP!
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
BrianMc1958Author Commented:
I not only can.  I did.  Thanks.  I'd like to point out that my memory in this case was at least in the ballpark--not out wandering around the interstate bypass in it's pajamas like usual.

--BrianMc1958
0
 
BrianMc1958Author Commented:
OK.  I'll respond in parallel.
OK.  I'll respond in parallel.

0
 
sciuriwareCommented:
Personally I tend to write a lot classes with static content (my assembler background?).

Then I give those classes a hidden constructor:

 private MyClass(){ /* Nothing */ }

.. to block faulty instantiation.

I state that this kind of classes is inevitable and right usage.

;JOOP!
0
 
sciuriwareCommented:
gggggggggggggggggggrr! That terrible CEHJ and you type faster than me!


;thanks!
0
 
BrianMc1958Author Commented:
I'm curious, why?  How can instantiation be faulty?  And why would a static block behave differently?
0
 
sciuriwareCommented:
I meant: such a class should not be instantiated at all. Sorry.
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.