0

In my java learning journey, I came across code that is commented with the following:

/**
 * Singleton instance - no need to lazy init as the class holds no state.
 */
public static final SuperParentMarshaller instance = new SuperParentMarshaller ();

What does this mean? What kind of class would this be? It's purpose?

Thank you in advance.

Horse Voice
  • 8,138
  • 15
  • 69
  • 120
  • Possible duplicate of http://stackoverflow.com/questions/3192095/where-exactly-the-singleton-pattern-is-used-in-real-application – Elliott Frisch Dec 10 '13 at 17:42
  • Are you asking what a singleton is? Or are you asking the difference between lazy and eager initialization? You should be able to find answers to both of those questions on stack overflow. – Sotirios Delimanolis Dec 10 '13 at 17:48
  • I don't understand what the logic behind the statement above is and how it will be used. In the class SuperParentMarshaller there is a variable being initialized as `public static final SuperParentMarshaller instance = new SuperParentMarshaller ();` What is this going to help with? I really am trying to understand the purpose of why it may be coded this way. – Horse Voice Dec 10 '13 at 20:07

2 Answers2

1

This is eager initialization what you have mentioned. The object is already initialized before the request to this object. To make it lazy means the object will be initialize on it's first call. This is a single design pattern. There will be only oneobject of this class in the entire application.

// eager loading of INSTANCE    
public class Singleton
{
//initailzed during class loading
private static final Singleton INSTANCE = new Singleton();

//to prevent creating another instance of Singleton
private Singleton(){}

public static Singleton getSingleton(){
    return INSTANCE;
}
}

Lazy Initialization is :

// lazy initialization
public class Singleton
{
//initailzed during class loading
private static final Singleton INSTANCE;

//to prevent creating another instance of Singleton
private Singleton(){}

public static Singleton getSingleton(){
    // object will be initialized on it's first call.
    if(INSTANCE == null)
        INSTANCE = new Singleton();
    return INSTANCE;
}
}
0

A singleton is to ensure there is one, and only ONE instance of that object present in your application. It can come in handy when for example to ensure there is only ONE audio class instance, so there can't be two instances asking the audio device to play two different things.

Though in practise, a singleton isn't used that much, it is handy to know it exists if you do ever come across the need of one.

The initialization method is than used to ensure only ONE instance can be created. There are several ways of ensuring this, if you DO NOT protect the instance from being created more than once, 2 threads could enter the class at the same time and you could end up with two instances of this class. Which would go against what you are trying to achieve.

Take a look at this: http://en.wikipedia.org/wiki/Singleton_pattern

Dylan Meeus
  • 5,696
  • 2
  • 30
  • 49