GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

GeekInterview.com  >  Tech FAQs  >  Programming  >  Java
Go To First  |  Previous Question  |  Next Question 
 Java  |  Question 7 of 885    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
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 1   Answer is not useful 0Overall Rating: +1    
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  |   Site Map  |   Add Question  |   Propose Category  |   RSS Feeds  |   Articles Sitemap  |   Site Updates  |   Add Resource

Copyright © 2005 - 2008 GeekInterview.com. All Rights Reserved
Page copy protected against web site content infringement by Copyscape