Interview Questions Answers.ORG
Interviewer And Interviewee Guide
Interviews
Quizzes
Home
Quizzes
Interviews Java Programming Language Interviews:Advanced JavaCore JavaEclipseFull Stack Developer (Java)HibernateIBM WebSphereInternationalization LocalizationJ2EEJ2MEJ2SEJavaJava ANTJava AppletJava BeansJava ClassesJava EJB ProgrammingJava Game DeveloperJava GUI FrameworkJava JNDIJava JNIJava JSP ProgrammingJava Message Service (JMS)Java Multi-ThreadingJava Networking - Sockets and RMIJava PatternsJava SecurityJava Server FacesJava Servlet ProgrammingJava Software EngineerJava Swing ProgrammingJava TechnicalJava ThreadsJava Transaction APIJava Web ServicesJavaMailJBossJDBCJMSJSFPortal and PortletRMISpring FrameworkSr.Java Web DeveloperStrutsSunOneSwing AWTSWT JFace
Copyright © 2018. All Rights Reserved
Java Classes Interview Question:
What is singleton class and how can we get it from a general class,explain with examples(program)?
Submitted by: AdministratorIn computer science, the singleton design pattern is designed to restrict instantiation of a class to one (or a few) objects. This is useful when exactly one object is needed to coordinate actions across the system. Sometimes it is generalized to systems that operate more efficiently when only one or a few objects exist.
The singleton pattern is implemented by creating a class with a method that creates a new instance of the object if one does not exist. If one does exist it returns a reference to the object that already exists. To make sure that the object cannot be instantiated any other way the constructor is made either private or protected.
The singleton pattern must be carefully constructed in multi-threaded applications. If two threads are to execute the creation method at the same time when a singleton does not yet exist, they both must check for an instance of the singleton and then only one should create the new one.
The classic solution to this problem is to use mutual exclusion on the class that indicates that the object is being instantiated.
A Java programming language solution is as follows. It is based on the Q&A link found below, modified for multi-threading, however, it is still vulnerable to the double-checked locking anti-pattern, also found below:
public class Singleton {
private static Singleton INSTANCE = null;
// Private constructor suppresses
// default public constructor
private Singleton() {}
//synchronized creator to defend against multi-threading issues
//another if check here to avoid multiple instantiation
private synchronized static void createInstance() {
if (INSTANCE == null) {
INSTANCE = new Singleton();
}
}
public static Singleton getInstance() {
if (INSTANCE == null) createInstance();
return INSTANCE;
}
}
Submitted by: Administrator
The singleton pattern is implemented by creating a class with a method that creates a new instance of the object if one does not exist. If one does exist it returns a reference to the object that already exists. To make sure that the object cannot be instantiated any other way the constructor is made either private or protected.
The singleton pattern must be carefully constructed in multi-threaded applications. If two threads are to execute the creation method at the same time when a singleton does not yet exist, they both must check for an instance of the singleton and then only one should create the new one.
The classic solution to this problem is to use mutual exclusion on the class that indicates that the object is being instantiated.
A Java programming language solution is as follows. It is based on the Q&A link found below, modified for multi-threading, however, it is still vulnerable to the double-checked locking anti-pattern, also found below:
public class Singleton {
private static Singleton INSTANCE = null;
// Private constructor suppresses
// default public constructor
private Singleton() {}
//synchronized creator to defend against multi-threading issues
//another if check here to avoid multiple instantiation
private synchronized static void createInstance() {
if (INSTANCE == null) {
INSTANCE = new Singleton();
}
}
public static Singleton getInstance() {
if (INSTANCE == null) createInstance();
return INSTANCE;
}
}
Submitted by: Administrator
Copyright 2007-2025 by Interview Questions Answers .ORG All Rights Reserved.
https://InterviewQuestionsAnswers.ORG.
https://InterviewQuestionsAnswers.ORG.