Solved

Instantiate a Singleton class with parameters in contructor

Posted on 2004-08-16
7
1,395 Views
Last Modified: 2006-11-17
I have a singleton class like

public class Singleton {

   private int innerInt;

   static private Singleton _instance = null;


   protected Singleton() {
   }

   static public Singleton instance() {
           if (_instance== null) {
      synchronized (Singleton.class) {
            if (_instance== null) {
                   _instance= new Singleton();
            }
      }
           }
           return _instance;
   }
}

The problem is that I need to set the value of innerInt in the constructor of the Singleton class when it is instantiated i.e. when instance() is called. Now i dont want everyone calling instance with a parameter.

I know one solution is to call instance() with a null parameter everytime but i was wondering if there is a more elegant solution to the problem.

Thanks.
0
Comment
Question by:thefallguy
  • 5
  • 2
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 11811603
Why not

static public Singleton instance(int i) {
    .....
}


?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 50 total points
ID: 11811617
>>Now i dont want everyone calling instance with a parameter.

You can always overload instance()
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11811637
You can also set the attribute using a mutator method

Singleton s = Singleton.instance();
s.setInnerInt(10);
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 35

Expert Comment

by:girionis
ID: 11811661
You could do:

protected Singleton() {
   if (innerInt == 0)
   {
        innerInt = 10;
    }
   }

this will work in all the cases apart the one that the user will try to set it to zero.
0
 
LVL 35

Expert Comment

by:girionis
ID: 11811691
On the other hand, the constructor will only be called once (at least per classloader) so you could simply set the value

protected Singleton() {
         innerInt = 10;
 
   }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11811763
Do you want the innerInt to be publicly mutable?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11812835
8-)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jar file executable 12 52
rhino JavaScript import, load 25 85
ejb on wildfly 5 20
Notify sent to other threads in Java 9 17
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question