What is singleton?

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









Related Tutorials/Questions & Answers:
ModuleNotFoundError: No module named 'singleton3'
Singletone utility in java
Advertisements
ModuleNotFoundError: No module named 'singletons'
ModuleNotFoundError: No module named 'django-singletons'
what is bit
what is hibernate.archive.autodetection
what is hibernate.archive.autodetection
What is procedure
what is hibernate.archive.autodetection
what is hibernate.archive.autodetection
what is hibernate.archive.autodetection
what is hibernate.archive.autodetection
What is Hibernate
What is Hibernate
What is Hibernate
What is Hibernate
What is JDBC?
What is FBT
What is ActionServlet?
What is Trigger?
What is VLR ?
What's PHP ?
What is an interface?
What is DML?
What is Index?
What is SQLLoader?
What is a "constraint"?
what is Savepoint ?
What is this keyword?
what is collections?
what are indices?
What is DataAccessException?
What is Externalizable?
What is the % operator?
What is casting?
What is IOC ?
What is SQLExceptionTranslator?
What are Struts?
What is WAP?
What is SPOOL?
What is CRM?
What is webservices?
What is Ajax?
What is NSRangeException
What is portal
what is posting?
What is a tuple?
What is workflow
what is RUP?
What was ENIAC

Ads