Solved

Instantiate a Singleton class with parameters in contructor

Posted on 2004-08-16
7
1,391 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
Comment Utility
Why not

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


?
0
 
LVL 86

Accepted Solution

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

You can always overload instance()
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
You can also set the attribute using a mutator method

Singleton s = Singleton.instance();
s.setInnerInt(10);
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 35

Expert Comment

by:girionis
Comment Utility
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
Comment Utility
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
Comment Utility
Do you want the innerInt to be publicly mutable?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
8-)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

744 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

15 Experts available now in Live!

Get 1:1 Help Now