CoolInterview.com - World's Largest Collection of Interview Questions & Answers, FAQs, queries, sample papers, exam papers, dumps, what, why, how, where, when questions
Our Services
Get 9,000 Interview Questions & Answers in an eBook.




Get it now !!
Send your Resume to 6000 Companies

Search Interview Questions

Question: What is Singleton class?

Answer: Singleton class is a class whose only one object can be created at a time. - To achieve this one method is that we make its constructor private and provide a static method that returns its instance.

When a Singleton class is garbage - collected and then reloaded, a new Singleton instance is created.

Submitted by Amit Mathur.



Category Java Interview Questions & Answers - Exam Mode / Learning Mode
Rating (1.7) By 225 users
Added on 8/8/2010
Views 4034
Rate it!
Question: What is Singleton class?
Answer:

Singleton class is a class whose only one object can be created at a time. - To achieve this one method is that we make its constructor private and provide a static method that returns its instance.

When a Singleton class is garbage - collected and then reloaded, a new Singleton instance is created.

Submitted by Amit Mathur.
Source: CoolInterview.com


The static method that is provided to get the instance should be thread safe by marking the method as synchronized Source: CoolInterview.com

Answered by: karthick | Date: 8/7/2009 | Contact karthick Contact karthick

singleton class is a classs in which at a time only one object can create. Source: CoolInterview.com

Answered by: priyanka yadav | Date: 9/28/2009 | Contact priyanka yadav Contact priyanka yadav

A singleton is an class that can be instantiated once, and only once. This is a fairly unique property, but useful in a wide range of object designs. Creating an implementation of the singleton pattern is fairly straightforward - simple block off access to all constructors, provide a static method for getting an instance of the singleton, and prevent cloning. Source: CoolInterview.com

Answered by: sowmya rani | Date: 10/22/2009 | Contact sowmya rani Contact sowmya rani

Singleton class is a class whose only one object can be created and only that object is used by JVM. Even you cannot creat clone for that object. Source: CoolInterview.com

Answered by: Prasann Kumar Mishra | Date: 11/2/2009 | Contact Prasann Kumar Mishra Contact Prasann Kumar Mishra

class A
{
int x=10;
private A()
{
private static A a=new A();
void m1()
{ system.out.println(x);
}
public static AgetA()
{ return a;
}
A a1=A.getA();
A a2=A.getA()'
}
HERE x=10 IS TO BE SHARE BY BOTH OF THE OBJECT. THIS TYPE OF CLASS IS CALLED SINGLETON DESIGN PATTRN.ONLY ONE OBJECT OF THIS CLASS CAN BE CREATED, WHICH IS SHARED BY ALL THE INSTANCES.WHEN WE WANT TO SHARE MEMORY THEN WE USED SINGLETON DESIGN PATTERN. Source: CoolInterview.com

Answered by: preeti raverkar | Date: 11/2/2009 | Contact preeti raverkar Contact preeti raverkar

A class which must create only one object (ie. One instance) irrespective of any number of object creations based on that class.
Steps to develop single ton class:-
1.Create a Private construcor, so that outside class can not access this constructor. And declare a private static reference of same class
2.Write a public Factory method which creates an object. Assign this object to private static Reference and return the object
Sample Code:-
class SingleTon
{
private static SingleTon st=null;
private SingleTon()
{
System.out.println(? In constructor?);
}
public static SingleTon stfactory()
{
if(st==null)
st=new SingleTon();
return st;
}
}
Source: CoolInterview.com

Answered by: Srikanth Doki | Date: 12/31/2009 | Contact Srikanth Doki Contact Srikanth Doki

Singleton class is a can be create at a time only one object but it can share memory Source: CoolInterview.com

Answered by: gokul | Date: 2/27/2010 | Contact gokul Contact gokul


A singleton is an class that can be instantiated once, and only once. This is a fairly unique property, but useful in a wide range of object designs. Creating an implementation of the singleton pattern is fairly straightforward - simple block off access to all constructors, provide a static method for getting an instance of the singleton, and prevent cloning. Source: CoolInterview.com

Answered by: sonu solanki | Date: 3/27/2010 | Contact sonu solanki Contact sonu solanki

class A
{

private static A a;
private A()
{

}
public static A getA()
{
if(a==null)
{
a=new A();
}
return a;
}

public static void main(String... s)
{
A a1=A.getA();
A a2=A.getA();
System.out.println(a1);

System.out.println(a2);
}

} Source: CoolInterview.com

Answered by: Rahul Negi | Date: 4/17/2010 | Contact Rahul Negi Contact Rahul Negi

singleton class is class which can create one object at a time.
it can also create an instance.it'll initiated at once. Source: CoolInterview.com

Answered by: bittu | Date: 7/10/2010 | Contact bittu Contact bittu

Singleton class is a class in which we can create a only one object of the class.Some time for the undestanding we can create a only one object for the user specification. Source: CoolInterview.com

Answered by: rahul shende | Date: 8/29/2010 | Contact rahul shende Contact rahul shende


If you have the better answer, then send it to us. We will display your answer after the approval

Rules to Post Answers in CoolInterview.com:-

  • There should not be any Spelling Mistakes.
  • There should not be any Gramatical Errors.
  • Answers must not contain any bad words.
  • Answers should not be the repeat of same answer, already approved.
  • Answer should be complete in itself.

Post your answer here

Inform me about updated answers to this question.
Related Questions
View Answer
What is JVM?
View Answer
What is JNI?
View Answer
What is JAR file?
View Answer
What is garbage collection?
View Answer
What is Socket. How will you create a connection?
View Answer
What is ServerSocket. How will you create a connection?
View Answer
What is DatagramSocket. How will you create a connection?
View Answer
What is Synchronisation in threads?
View Answer
What is Collection?
View Answer
What is JavaMail?
View Answer
What is Serialization and Deserialization?
View Answer
What is the life cycle of an applet?
View Answer
What is Reflection. Uses of Reflection?
View Answer
What is the purpose of finalisation?
View Answer
What is the functionality of try, catch and finally blocks?
View Answer
What is the difference between Abstract class and Interface?
View Answer
What is the difference between overloading and overriding?
View Answer
What is the difference between Hashmap and Hashtable?
View Answer
What is the difference between Vector, Array and Arraylist?
View Answer
What is the difference between final, finally and finalized?
View Answer

Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.

View All Java Interview Questions & Answers - Exam Mode / Learning Mode




India News Network
Latest 20 Questions
Payment of time- barred debt is: (a) Valid (b) Void (c) Illegal (d) Voidable
Consideration is defined in the Indian Contract Act,1872 in: (a) Section 2(f) (b) Section 2(e) (c) Section 2(g) (d) Section 2(d)
Which of the following is not an exception to the rule, "No consideration, No contract": (a) Natural love and affection (b) Compensation for involuntary services (c) Completed gift (d) Agency
Consideration must move at the desire of: (a) The promisor (b) The promisee (c) The promisor or any other party (d) Both the promisor and the promisee
An offer which is open for acceptance over a period of time is: (a) Cross Offer (b) Counter Offer (c) Standing Offer (d) Implied Offer
Specific offer can be communicated to__________ (a) All the parties of contract (b) General public in universe (c) Specific person (d) None of the above
_________ amounts to rejection of the original offer. (a) Cross offer (b) Special offer (c) Standing offer (d) Counter offer
A advertises to sell his old car by advertising in a newspaper. This offer is caleed: (a) General Offer (b) Special Offer (c) Continuing Offer (d) None of the above
In case a counter offer is made, the original offer stands: (a) Rejected (b) Accepted automatically (c) Accepted subject to certain modifications and variations (d) None of the above
In case of unenforceable contract having some technical defect, parties (a) Can sue upon it (b) Cannot sue upon it (c) Should consider it to be illegal (d) None of the above
If entire specified goods is perished before entering into contract of sale, the contract is (a) Valid (b) Void (c) Voidable (d) Cancelled
______________ contracts are also caled contracts with executed consideration. (a) Unilateral (b) Completed (c) Bilateral (d) Executory
A offers B to supply books @ Rs 100 each but B accepts the same with condition of 10% discount. This is a case of (a) Counter Offer (b) Cross Offer (c) Specific Offer (d) General Offer
_____________ is a game of chance. (a) Conditional Contract (b) Contingent Contract (c) Wagering Contract (d) Quasi Contract
There is no binding contract in case of _______ as one's offer cannot be constructed as acceptance (a) Cross Offer (b) Standing Offer (c) Counter Offer (d) Special Offer
An offer is made with an intention to have negotiation from other party. This type of offer is: (a) Invitation to offer (b) Valid offer (c) Voidable (d) None of the above
When an offer is made to the world at large, it is ____________ offer. (a) Counter (b) Special (c) General (d) None of the above
Implied contract even if not in writing or express words is perfectly _______________ if all the conditions are satisfied:- (a) Void (b) Voidable (c) Valid (d) Illegal
A specific offer can be accepted by ___________. (a) Any person (b) Any friend to offeror (c) The person to whom it is made (d) Any friend of offeree
An agreement toput a fire on a person's car is a ______: (a) Legal (b) Voidable (c) Valid (d) Illegal
Cache = 0.9375 Seconds