Jitendra
Jitendra

Reputation: 109

java Reuse existing object

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.

Upvotes: 1

Views: 987

Answers (5)

Yogendra Singh
Yogendra Singh

Reputation: 34367

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;
     }
}

Upvotes: 0

CaTalyst.X
CaTalyst.X

Reputation: 1665

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.

Upvotes: 0

Bhesh Gurung
Bhesh Gurung

Reputation: 51030

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.

Upvotes: 1

jlordo
jlordo

Reputation: 37813

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

Upvotes: 0

Patricia Shanahan
Patricia Shanahan

Reputation: 26185

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.

Upvotes: 0

Related Questions