1

I can solve this problem using singleton pattern. But problem is I don't have control on other application which is going to call new MyClass().
Is there any way I can do in implicit constructor of MyClass?.
Something like this.

class ClassName {
    public ClassName() {
        if( object exist for ClassName)
            return that
        else
            create New ClassName object
    }
}

Thanks in advance.

1
  • Look at Singleton pattern. You have to hide the standard constructor, so no one else can call it. Commented Nov 6, 2012 at 6:47

5 Answers 5

1

You can use a enum:

public enum ClassName {
    INSTANCE;

}

Now, you have one instance and you don't have to worry about others instantiating your class.


Is there any way I can do in implicit constructor of MyClass?.

No, that can't be done in a constructor.

Sign up to request clarification or add additional context in comments.

Comments

0

If you want to control construction, put in an explicit constructor and declare it private. You can call it from a static factory method, in the class.

Comments

0

This is probably what you want:

public class MySingletonClass {
    private static MySingletonClass instance = null;
    private MySingletonClass() { }
    public static MySingletonClass getInstance() {
       if (instance == null) {
           instance = new MySingletonClass();
       }
       return instance;
    }
    // add your methods here.
}

This way nobody can call new MySingletonClass();. To get the one and only instance of the object you have to write:

MySingletonClass msc = MySingletonClass.getInstance();

or use it somehow like this for void methods:

MySingletonClass.getInstance().yourMethod();

or like this for Methods with a return type:

VariableType foo = MySingletonClass.getInstance().yourMethod(); // Must return VariableType

Comments

0

You would need something like this:

class ClassName {

private static ClassName INSTANCE;

private ClassName() {
    //create ClassName object
}

public static ClassName getInstance(){
if (INSTANCE == null){
    INSTANCE = new ClassName();
} 

return INSTANCE;
}

}

Which is just a basic implementation of the singleton pattern.

If the class that constructs the object HAS to construct it using new, then you are kind of screwed. There is really no way to implement a singleton pattern in Java using only a public constructor.

Edit: You might be able to do something like this:

class ClassNameWrapper extends ClassName {

private final ClassName className;

public ClassNameWrapper(){
    className = ClassName.getInstance();
}

//overload methods from ClassName

}

This way, every call to new ClassNameWrapper() will be using the same instance of ClassName.

Comments

0

Create a static variable in your class and hold your object instance there. Expose you class object through a getter method as below:

  class ClassName {

     private static ClassName myClass= null;

     public ClassName getClassName() {
       if(myClass == null){
         ClassName.myClass = new ClassName();
       }
       return ClassName.myClass;
     }
}

2 Comments

This doesn't really solve the problem. That is like the opposite of a singleton. It guarantees that every thread will have it's own copy.
@user1522691: Remove the ThreadLocal and use the static variable of your class type itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.