GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Tech FAQs  >  Programming  >  Java
Go To First  |  Previous Question  |  Next Question 
 Java  |  Question 7 of 928    Print  
What is singleton class & it's implementation.

  
Total Answers and Comments: 5 Last Update: September 27, 2006   
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: Vinoth Senthill
 
With the Singleton design pattern you can:  
Ensure that only one instance of a class is created 
Provide a global point of access to the object 
Allow multiple instances in the future without affecting a singleton class's clients 
 
//Sample Code 
public class SingleInstance { 
private static SingleInstance ourInstance = new SingleInstance(); 
 
public static SingleInstance getInstance() { 
if(null==ourInstance){ 
ourInstance= new SingleInstance(); 

return ourInstance; 

private SingleInstance() { 




Above answer was rated as good by the following members:
shilpad, shyjuck
July 01, 2005 05:20:33   #1  
Vinoth Senthill        

RE: What is singleton class & it's implementation.
With the Singleton design pattern you can:
Ensure that only one instance of a class is created
Provide a global point of access to the object
Allow multiple instances in the future without affecting a singleton class's clients

//Sample Code
public class SingleInstance {
private static SingleInstance ourInstance new SingleInstance();

public static SingleInstance getInstance() {
if(null ourInstance){
ourInstance new SingleInstance();
}
return ourInstance;
}
private SingleInstance() {
}
}

 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 0Overall Rating: +2    
September 08, 2005 00:40:41   #2  
Rose        

RE: What is singleton class & it's implementation.
Nice explanation.... usage of singleton classes can also be included..
 
Is this answer useful? Yes | No
October 18, 2005 06:59:33   #3  
Hemanth Kumar Venugobal        

RE: What is singleton class & it's implementation...
good
 
Is this answer useful? Yes | No
September 12, 2006 07:47:52   #4  
artika        

then why class

when only one instance of a class can be made

then wat's the purpose of makin it as a class


 
Is this answer useful? Yes | No
September 27, 2006 04:35:32   #5  
chandrakanth        

RE: What is singleton class & it's implementation...

In singleton class only one time the instance wil create.Example....

public class Sin
{
static Sin s null;
public static void main(String a[])
{
create();
create();
create();
}
public static Sin create()
{
if(s null)
{
s new Sin();
}
else
{
System.out.println( Please it is a single ton class );
}
return s;
}
private Sin()
{
System.out.println( Single ton class );
}
}


 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape