ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage   interview questions urls   External Links  Contact Us     Login  |  Sign Up                      
tip   To Refer this Site to Your Friends   Click Here
Google
 
Categories  >>  Software  >>  Java J2EE  >>  Java Related  >>  Java J2EE AllOther
 
 


 

 
 Core Java interview questions  Core Java Interview Questions
 Advanced Java interview questions  Advanced Java Interview Questions
 Swing interview questions  Swing Interview Questions
 EJB interview questions  EJB Interview Questions
 Servlets interview questions  Servlets Interview Questions
 Struts interview questions  Struts Interview Questions
 JDBC interview questions  JDBC Interview Questions
 JMS interview questions  JMS Interview Questions
 SunOne interview questions  SunOne Interview Questions
 J2EE interview questions  J2EE Interview Questions
 Weblogic interview questions  Weblogic Interview Questions
 Websphere interview questions  Websphere Interview Questions
 Java Networking interview questions  Java Networking Interview Questions
 Java J2EE AllOther interview questions  Java J2EE AllOther Interview Questions
Question
How to implement or use the singleton class in java?
 Question Submitted By :: Ragini3
I also faced this Question!!     Rank Answer Posted By  
 
  Re: How to implement or use the singleton class in java?
Answer
# 1
A singleton is an object that cannot be instantiated. At
first, that might seem counterintuitive - after all, we need
an instance of an object before we can use it. Well yes a
singleton can be created, but it can't be instantiated by
developers - meaning that the singleton class has control
over how it is created. The restriction on the singleton is
that there can be only one instance of a singleton created
by the Java Virtual Machine (JVM) - by prevent direct
instantiation we can ensure that developers don't create a
second copy.

We'll start with the class definition, for a SingletonObject
class. Next, we provide a default constructor that is marked
as private. No actual code needs to be written, but you're
free to add some initialization code if you'd like. 

public class SingletonObject
{
	private SingletonObject()
	{
		// no code req'd
	}
}

So far so good. But unless we add some further code,
there'll be absolutely no way to use the class. We want to
prevent direct instantiation, but we still need to allow a
way to get a reference to an instance of the singleton object.
 
Is This Answer Correct ?    0 Yes 0 No
Pinaki Mukherjee
 
  Re: How to implement or use the singleton class in java?
Answer
# 2
singleton is a design pattern. in this per application only 
one object should exist.we implement it as follows:

public class abcd
{
private static abcd instance=null;
protected abcd()
{
//only to defeat instantiation;
}
public static abcd getInstance()
{
if(instance==null)
{
instance=new abcd();
}
return instance;
}
 
Is This Answer Correct ?    1 Yes 0 No
Amit Sharma
 
 
 

 
 
 
Other Java J2EE AllOther Interview Questions
 
  Question Asked @ Answers
 
What is the use of log4j and how to make use of that in a application?  1
what is difference between business deligate and session facade ? Fidelity2
what are callback methods? iFlex2
httt method HCL1
what is diff string and stringbuffer HCL4
what debugging tool that can be used to debug the java programs? Inforica1
What is an Ioc pattern? There are assemblies in .net for establishing this task, Whats the special in Java /j2ee technologies about IOC  2
VSS Objectives  1
How do you debug your java program? Inforica1
what protocols does JNDI provide an interface to?  1
Is it possible to create Userdefined Unchecked Exception also?If Yes, give an example?  1
Why do you use Context Object HCL2
What is a constructor?  2
what is the IDE that you have used to write your java programs? Inforica2
what is the difference between banking and insurance domain?  1
How to use ANT?  1
what is difference between vector and arraylist?. HCL2
who should use JNDI?  1
where is JNDI being used in java platform?  2
whats the relation ship between LDAP and JNDI?  1
 
For more Java J2EE AllOther Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com