?
Solved

Instantiate a Singleton class with parameters in contructor

Posted on 2004-08-16
7
Medium Priority
?
1,402 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

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.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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:
Suggested Courses
Course of the Month10 days, 12 hours left to enroll

765 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