Solved

Instantiate a Singleton class with parameters in contructor

Posted on 2004-08-16
7
1,394 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum5 challenge 5 69
splitOdd10 challenge 5 80
fibonacci ten numbers 4 28
Way to decrease size of apk file 9 42
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

910 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now