• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • 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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
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
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.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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