• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 167
  • Last Modified:

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
0
BrianMc1958
Asked:
BrianMc1958
  • 5
  • 3
  • 2
2 Solutions
 
CEHJCommented:
You could do

static
  {
    names[BOB_INDEX] = "Bob";
    names[ALICE_INDEX] = "Alice";
  }
0
 
sciuriwareCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now