GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Tech FAQs  >  Programming  >  Java
Go To First  |  Previous Question  |  Next Question 
 Java  |  Question 870 of 928    Print  
Java Singleton Class
How to create a class that has only one object?


  
Total Answers and Comments: 2 Last Update: July 10, 2008     Asked by: sumon_123 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: atmarammuduli
 
There r several way to create Singleton object:
1. By using single static private object & using a static method while returning same object on every call.
2. using a static boolean flag for checking the internal static singleton object is initialized or not and returning same object if it is true.
3. (Advanced) Using ThreadLocal class. This is real singleton object. No other factory classs cant create same object again.

Above answer was rated as good by the following members:
sreehari434
July 10, 2008 02:56:57   #1  
atmarammuduli Member Since: December 2007   Contribution: 8    

RE: Java Singleton Class
There r several way to create Singleton object:
1. By using single static private object & using a static method while returning same object on every call.
2. using a static boolean flag for checking the internal static singleton object is initialized or not and returning same object if it is true.
3. (Advanced) Using ThreadLocal class. This is real singleton object. No other factory classs cant create same object again.

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
July 10, 2008 03:05:09   #2  
atmarammuduli Member Since: December 2007   Contribution: 8    

RE: Java Singleton Class
refer my previos rep:

Method 3 is a way to create per thread singleton object. ie. every thread will have its own singleton object for its own disposal.

Method 2 1 will fall into a waiting state for singleton object until freed from other thread adding to performance issue.

 
Is this answer useful? Yes | No

 Related Questions

As of now ,what i know abt cloning is that we can make a field to field copy of an object.ie when we clone an object,its state as well as a its behaviour is copied to the cloned object.My question is what is the neccesity of it? cant we create another object of the same class? so how does its different from cloned object (that is creating another object of that class rather than  being cloned).is there any security issues coupled with this cloned object? And What is the Concrete purpose of

Latest Answer : No it won't work.Only one main methods is allowed ...

Latest Answer :  In c,there are argc and argv.argc is passed to argv and argv is used to store characters.in java there is only args.we pass a String value to it. ...

Latest Answer : Abstract class can have instance variable, static variable, constructor, abstract methods, concrete methods, instance blocks and static blocks.Subclass has to extend abstract class using extend keyword. ...
Tags : Abstract

Latest Answer : 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();     ...

Latest Answer : Below are three signatures:public final void notify()public final void notifyAll()public final void wait(long timeout) ...

Latest Answer : A Java Bean is a reusable software component that can be visually manipulated in builder tools.  To understand the precise meaning of this definition of a Bean, clarification is required for the following terms:   1. Software component: Reusable ...

Latest Answer : yes ...

Latest Answer : Initially it didnt as java uses "extends" word for inheritance but it didnt allow us to extend more then one class.So the concept of "interface" came which is like abstrct class but with strictness that methods will only be declared at that place & have ...

Latest Answer : JasperReports is a powerful open source Java reporting tool that has the ability to deliver rich content onto the screen, to the printer or into PDF, HTML, XLS, CSV and XML files.Check this link out http://java-source.net/open-source/charting-and-reporting/jasperreports ...


 Sponsored Links

 
Related Articles

Service Oriented Java Business Integration Review

Service Oriented Java Business Integration Review Introduction If you ve read through the texts which give you an introduction to SOA or Web Services you will often find them to be quite frustrating and the reason for this is because they spend too much time referencing business processes which are
 

Concepts of Object-Oriented Programming

Object Oriented JavaScript In this chapter you ll learn about OOP Object Oriented Programming and how it relates to JavaScript As an ASP NET developer you probably have some experience working with objects and you may even be familiar with concepts such as inheritance However unless you re already a
 

C++ Pure Virtual Function and Base Class

C Pure Virtual Function and Virtual Base Class In this C tutorial you will learn about pure virtual function declaration of pure virtual function and virtual base class virtual base class and how to implement a virtual base class explained with examples mosgoogle center What is Pure Virtual Function
 

Java and Client Server Models

Java and Client Server Models The Role of Client Servers on the Web Client server models provide the essential mechanisms for working with the Internet In fact most of the World Wide Web is built according to this paradigm In client server models the web browsers run by millions of users are the cli
 

How to Access C++ Class Members

How to Access C Class Members In this C tutorial you will learn how to access Class members dot operator or class member access operator difference between struct and class and  scope resolution operator mosgoogle center It is possible to access the class members after a class is defined an
 

SQL Programming

SQL Programming Overview Anybody who has done something for a long time has probably wanted to change how things work at some point or another. A worker at a mill might have found a more efficient way of cutting logs, or a mathematics teacher might have had a hand in changing a school’s al
 

WinRunner Programming Concepts

If you want to create WinRunner scripts that are highly efficient, there are important programming concepts that you will want to become familiar with. Understanding these concepts will provide you with a large number of key benefits. In addition to understanding these concepts, you must also learn
 

Programming Languages Certification

IT Certification programs have several options that will offer you the best knowledge.  By learning everything that you need to know about information technology you will be able to open new doors to your career and personal business desires.  IT Certification offers several vari
 

Using UML with Java

Using UML with Java While Java is not a new language its application for the development of embedded systems is quite new Developers are beginning to take a second look at modeling languages such as UML and many feel it can be a powerful tool in their development arsenal mosgoogle center Introductio
 

Neuro-linguistic Programming Methods

Neuro linguistic Programming Methods There are several methods used for performing Neuro linguistic Programming on an individual for obtaining insights into the psyche of the person in order to correct to modify certain patterns of behavior These techniques are also used for Neuro linguistic trainin
 

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