Q) I know about the singleton objects and how we can achieve this.
A)
One of the way to achieve your objective is
1. Declare the constructor as private static final.
2. Declare one method as static with return current class (example getInstance as shown.)
3. create required methods as per the requirement.
public SingletonTest{
private static final SingletonTest SINGLETON = new SingletonTest();
priavate SingletonTest()
{}
public static SingletonTest getInstance()
{
return SINGLETON;
}
public TestPrint(
String strName)
{
System.out.println("Name passed is "+ strName);
}
}
Q)what are the advantages/disadvantages over the class as I describe above?
Many times you might require only one instance of class is made used by a application during its execution.
Example one part of the application set some value to the class variable and some other point of time another part of application want the value same value set. Since same instance is serviced for all request thought out the application execution. This can be easily achieved by following Singleton Object.
Using singleton model when you really don�t require it and ending up in a mess is the disadvantage part which will not occur if it is understood and used.