This discussion is archived
8 Replies Latest reply: Feb 15, 2011 8:54 AM by 796440 RSS

Can a object created using static keyword be recreated? Singleton Pattern

798609 Newbie
Currently Being Moderated
Hi All

To implement a Singleton I can use the following code

public Singleton {

private Singleton(){
}

private static final s_instance = new Singleton();

public Singleton getInstance(){
return s_instance;
}

Now if the Class Singleton gets unloaded during a program execution and again get loaded won't s_instance be recreated? If my question is correct this would lead to be leading to a new Singleton class instance with all the original variables.

Having answered the above question if I lazily initialize my Singleton using the following code won't the above problem still occur.

public Singleton {
private Singleton(){}

private final volatile Singleton s_instance = null;

public Singleton () {
if(s_instance != null) {
synchronized (s_instance) {
s_instance = new Singleton();
}
return s_instance;
}
}



Having answered all the above questions what is the best way to write a Singleton?
  • 1. Re: Can a object created using static keyword be recreated? Singleton Pattern
    tschodt Pro
    Currently Being Moderated
    user9546108 wrote:
    To implement a Singleton I can use the following code
    public Singleton {
    private Singleton() {}
    private static final s_instance = new Singleton();
    public Singleton getInstance() {
    return s_instance;
    }
    }
    Now if the Class Singleton gets unloaded during a program execution
    That can only happen if the ClassLoader that loaded the Class is unloaded.
    The system classloader is never unloaded.
  • 2. Re: Can a object created using static keyword be recreated? Singleton Pattern
    798609 Newbie
    Currently Being Moderated
    Thanks for your reply.

    Would the class get unloaded even if the Singleton object has some valid/reachable references pointing to it?

    Will the same case (Custom ClassLoader getting unloaded) occur if I write the singleton using the code wherein we lazily initialize the Singleton (and actually create a Singleton instance rather than it being a Class/static variable).

    On a different note, will all the objects/instances of a class get destroyed if the class gets unloaded.

    Thanks
  • 3. Re: Can a object created using static keyword be recreated? Singleton Pattern
    jduprez Pro
    Currently Being Moderated
    Would the class get unloaded even if the Singleton object has some valid/reachable references pointing to it?
    On a different note, will all the objects/instances of a class get destroyed if the class gets unloaded.
    That's the other way round, really: instances cannot be garbage-collected as long as they are strongly reachable (at least one reference pointing to them), and classes cannot be unloaded as long as such an instance exists.
    To support tschodt's point, see the JVM specifications: http://java.sun.com/docs/books/jvms/second_edition/html/Concepts.doc.html#32202
    *2.17.8 Unloading of Classes and Interfaces*
    A class or interface may be unloaded if and only if its class loader is unreachable. The bootstrap class loader is always reachable; as a result, system classes may never be unloaded.
    Will the same case (Custom ClassLoader getting unloaded) occur if I write the singleton using the code wherein we lazily initialize the Singleton (and actually create a Singleton instance rather than it being a Class/static variable).
    You should check the meaning of this vocabulary ("object", "instance", "reference"): http://download.oracle.com/javase/tutorial/java/javaOO/summaryclasses.html
    The difference between the lazy vs eager initializations is the time when an instance is created. Once it is created, it will not be garbage collected as long as it is reachable.

    Now I'm wondering, whether being referenced by a static attribute of a class guarantees being reachabe. I'm afraid not (see the JLS definition of reachable: http://java.sun.com/docs/books/jls/third_edition/html/execution.html#44762). That is, again, unless the class has been loaded by the system class loader.
  • 4. Re: Can a object created using static keyword be recreated? Singleton Pattern
    tschodt Pro
    Currently Being Moderated
    user9546108 wrote:
    Would the class get unloaded even if the Singleton object has some valid/reachable references pointing to it?
    No. An instance of an object has a reference to its class.
    Will the same case (Custom ClassLoader getting unloaded) occur if I write the singleton using the code wherein we lazily initialize the Singleton (and actually create a Singleton instance rather than it being a Class/static variable).
    If you create an instance and reference it from a class field (a static reference variable)
    then the instance will not be eligible for GC until the class is.
    On a different note, will all the objects/instances of a class get destroyed if the class gets unloaded.
    The class will not get unloaded if instances exist that are not eligible for GC.

    If objects are no longer reachable, possibly, but why would you care whether they are destroyed if you cannot reach them.
      [url https://groups.google.com/group/comp.lang.java.programmer/msg/7889879d8cbb089f?hl=en&pli=1]GC parable ^[url https://groups.google.com/group/comp.lang.java.programmer/msg/7889879d8cbb089f?hl=en&pli=1]usenet^
  • 5. Re: Can a object created using static keyword be recreated? Singleton Pattern
    798609 Newbie
    Currently Being Moderated
    Thanks guys ... So the answer to my question of writing the robust possible Singleton would be initializing it lazily and not referencing it from a static variable since in the later case the Singleton would be at the mercy of the ClassLoader being GarbageCollected.
  • 6. Re: Can a object created using static keyword be recreated? Singleton Pattern
    tschodt Pro
    Currently Being Moderated
    user9546108 wrote:
    Thanks guys ... So the answer to my question of writing the robust possible Singleton would be
    using an Enum
    and not referencing it from a static variable since in the later case the Singleton would be at the mercy of the ClassLoader being GarbageCollected.
    Huh?
    If the class is on the classpath of the system class loader
    the class will never be eligible for GC
    as the system class loader will never be eligible for GC.
  • 7. Re: Can a object created using static keyword be recreated? Singleton Pattern
    796440 Guru
    Currently Being Moderated
    user9546108 wrote:
    Thanks guys ... So the answer to my question of writing the robust possible Singleton would be initializing it lazily
    There's no good reason to ever lazily instantiate a singleton.
    and not referencing it from a static variable since in the later case the Singleton would be at the mercy of the ClassLoader being GarbageCollected.
    Eh? You never have to worry about a class in use being GCed. If the class or any static or non-static member variable is being referenced or any method is being executed, it cannot be GCed. The only way the class can be GCed is if its ClassLoader is GCed. That can only happen if its a ClassLoader that you explicitly created (that is, NOT the one that loads things off classpath automatically--it's never GCed), and only if neither that ClassLoader nor any class it has loaded is being referenced.

    And if your singleton isn't in use, then why would you care if it's GCed? (Highly unlikely to happen unless you go out of your way to make it possible anyway.)

    It really sounds like you've missed the point of what people have been saying.
  • 8. Re: Can a object created using static keyword be recreated? Singleton Pattern
    796440 Guru
    Currently Being Moderated
    tschodt wrote:
    user9546108 wrote:
    Thanks guys ... So the answer to my question of writing the robust possible Singleton would be
    using an Enum
    Or a traditional eagerly instantiated singleton pattern. (I personally don't like the enum-as-singleton approach. It seems like an abuse of enums to me. But that's 95% subjective.)

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points