What is singleton?

Ads
 

What is singleton?

Hi,

I want to know more about the singleton in Java. Please explain me what is singleton in Java? Give me example of Singleton class.

Thanks
View Answers

October 5, 2010 at 4:50 PM

hello,
Singleton is a concept in which you can create only object of a class.
You can achive this features by making class constructor private, and write a single static method (single entry) for object creation.
ex.
public class SingletonExample{
public static SingletonExample obj= null;
private SingletonExample(){
}
public static SingletonExample getClassObj(){
if(obj == null){
obj = new SingletonExample();
}
return obj;
}

}
Test class
public class test{
public static void main(String arg[]){
System.out.println(SingletonExample.getClassObj().toString);
System.out.println(SingletonExample.getClassObj().toString);
}
}

o/p both print same string representation of obj

Let me know if u have any doubt on this.
Thanks

Ads









Related Tutorials/Questions & Answers:
Singletone utility in java
what is bit
Advertisements
What is procedure
what is hibernate.archive.autodetection
What is ActionServlet?
What is Trigger?
What is DML?
What is Index?
What is a "constraint"?
What is this keyword?
what is collections?
What is Hibernate
What is JDBC?
What is FBT
What is VLR ?
What's PHP ?
What is an interface?
What is SQLLoader?
what is Savepoint ?
what are indices?
What is a transaction and what are ACID properties?
What is an Abstract Class and what is it's purpose?
What is Externalizable?
What is the % operator?
What is DataAccessException?
What is SQLExceptionTranslator?
What is casting?
What is IOC ?
What are Struts?
What is WAP?
What is a tuple?

Ads