[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 196
  • Last Modified:

NEWBIE: How to use constructors to fill Static fields?

Dear Experts,

I have a class that basically just fills in and stores Static fields.  The first time it's instantiated, I want the constructor to actually fill in the Static fields (by reading values from a text file, for instance).  In subsequent instantiations, I don't want the constructor to do anything.  (I just want to get the values of the Static fields...)

Currently, I'm using two different constructors to accomplish this.  To distinguish them, one accepts an unused String parameter, and the other has no parameter.  This works, but it seems ugly.  Is there a more elegant, industry-standard way to accomplish this?  I was thinking, for instance, of just checking one of the Static fields for null, and then doing the real initialization...

Thanks,
BrianMc1958
0
BrianMc1958
Asked:
BrianMc1958
2 Solutions
 
koppchaCommented:
Check for not null all the time..If it is null then only initialize it.
0
 
koppchaCommented:
You can also use static initializers .Please go through this document
http://www.unix.org.ua/orelly/java-ent/jnut/ch03_02.htm
0
 
BrianMc1958Author Commented:
Dear koppcha:

Thanks.  But I'm still confused!  What is the advantage of static initializers over normal constructors?  It seems to me they do the same things!

Thanks,
BrianMc1958
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.

 
StillUnAwareCommented:
Here is an example of static initialization, which is performed only once while loading the class.

import java.io.*;

public class StaticInit {
  static String s;
  static {
    try {
      BufferedReader br = new BufferedReader(new FileReader("init.txt"));
      s = br.readLine();
    } catch(IOException ioe) {
      ioe.printStackTrace();
      throw new RuntimeException(ioe);
    }
  }
  public static void main(String[] args) {
    System.out.println(s);
  }
}
0
 
koppchaCommented:
If you are using static initializer it can be initialized by reading from a file as suggested by "StillUnAware" but it is not a good idea to initialize that in the constructor because static is a class varibale its value is the same across different instances.But constructor is something specific to the instance of a class..
0
 
objectsCommented:
If you're only dealing with static class variables then you do not even need a constructor (or create an instance of the class at all)
0
 
limaidealCommented:
I will prefer to create a static init(String) method to explicitly do the initialization, and make sure you call it before any other usage of this class.

If you insist to use one constructor to do both init and following construction, you can simply check the static variable against null. If == null, do init, otherwise skip init. Make sure synchronize the code that is performing the check.
0
 
BrianMc1958Author Commented:
Thank you, everybody!  That's plenty of answers for such a small question!

--BrianMc1958
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now