HOME FORUM BLOGS CONTACT LINKS FEEDBACK LOGIN REGISTER
 

JAVA DEVELOPER SITE - COMPLETE JAVA TUTORIALS

Singleton Pattern in Java



Newly Added TutorialNewly Added Tutorial


Java Exception Handling
Factory Design Pattern
Thread Creation in Java
Read a File in Java
Convert List to Array
Read Excel in Java
Setting thread priorities
Method Invocation Using Reflection
Ajax Basics
Stateless Session Beans Callback
Ajax Autocomplete in Java
Hello World Java
Hello World Java
Decision Table
Object Serialization
Singleton Pattern
Java Heap Size
Set Heap Size Eclipse
OutofMemory Exception in Java
Axis Web Services
Java C++ Difference
SOA Tutorials
Filenet Platform BPM
Eclipse Tips & Tricks
enterprise service bus (ESB)
Enterprise Application Integration (EAI)

JAVA Tutorials

Collection Framework
Operators in Java
If-else Statement

Thread
File
Generics
Input-Output in java
File Stream
Java Methods
Java Arrays
Java Objects
Java Exception Handling
Java Inheritance Tutorial
Java Threads
Garbage collection

J2EE Tutorials

Servlets Tutorials
Introduction to Struts Framework
JSP Tutorials
SPRING Framework Tutorials
STRUTS Tutorials
HIBERNATE Tutorials
WEB-SERVICES Tutorials

Java Collections

MAP
SET
LIST
VECTOR

Advanced Java

Stateless Session Bean
Introduction to EJB
Stateful Session Bean
Java Design Patterns

BRMS Tutorial

ILOG
BLAZE ADVISOR
PEGA
Drools
BRMS
BPM
JRules for .NET
ilog jrules interview questions
ilog jrules certification dumps
Java Certification Study Guide
hibernate tutorial
hello world struts example

All Tutorial

php mysql tutorial
SCJP 1.5/1.6
SCWCD 1.5/1.6
Polymorphism
Thread Tutorial
Generics & Exceptions
Spring Struts Hibernate

 
 
 

Useful Java Links




Implementing Singleton Pattern

To implement this design pattern we need to consider the following 4 steps:

 
Step 1: Provide a default Private constructor

public class SingletonObjectDemo {

	// Note that the constructor is private
	private SingletonObjectDemo() {
		// Optional Code
	}
}

Step 2: Create a Method for getting the reference to the Singleton Object

public class SingletonObjectDemo {

	private static SingletonObject singletonObject;
	// Note that the constructor is private
	private SingletonObjectDemo() {
		// Optional Code
	}
	public static SingletonObjectDemo getSingletonObject() {
		if (singletonObject == null) {
			singletonObject = new SingletonObjectDemo();
		}
		return singletonObject;
	}
}

We write a public static getter or access method to get the instance of the Singleton Object at runtime. First time the object is created inside this method as it is null. Subsequent calls to this method returns the same object created as the object is globally declared (private) and the hence the same referenced object is returned.

Step 3: Make the Access method Synchronized to prevent Thread Problems.

public static synchronized SingletonObjectDemo getSingletonObject()

It could happen that the access method may be called twice from 2 different classes at the same time and hence more than one object being created. This could violate the design patter principle. In order to prevent the simultaneous invocation of the getter method by 2 threads or classes simultaneously we add the synchronized keyword to the method declaration

Step 4: Override the Object clone method to prevent cloning

We can still be able to create a copy of the Object by cloning it using the Object’s clone method. This can be done as shown below

SingletonObjectDemo clonedObject = (SingletonObjectDemo) obj.clone();

This again violates the Singleton Design Pattern’s objective. So to deal with this we need to override the Object’s clone method which throws a CloneNotSupportedException exception.

public Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}

Transient Fields and Java Serialization

The transient keyword is a modifier applied to instance variables in a class. It specifies that the variable is not part of the persistent state of the object and thus never saved during serialization.



If you need any urgent assistance on Implementing_Singleton_Pattern_In_Java, kindly email your requirement to us at : info@javagenious.com or Contact-an-Expert. Our experts will try their best to solve your problem.

Keyword Tags: Implementing_Singleton_Pattern_In_Java tutorial,Implementing_Singleton_Pattern_In_Java in java,Concepts of Implementing_Singleton_Pattern_In_Java,Java,J2EE,Interview Questions,Implementing_Singleton_Pattern_In_Java Examples


Java Virtual Machine (JVM)

Most programming languages compile source code directly into machine code, suitable for execution on a particular microprocessor architecture. read more


Java Related Discussions

 
Javagenious.com

Interview Questions
Placement Papers
FAQs on J2EE
FAQs on JNDI
XML-Tutorials
Remote Web Services
Exception-Handling
JMS Tutorials
seam richfaces tutorial
richfaces live demo
jquery example demo
JQuery Tutorial and Source Code
JSF Tutorial
XSL Tutorial
J2EE JMS
CORBA Applications
Java CERTIFICATION
SCJP 1.5
SCJP 1.6
SCWCD 1.5
SCBCD
Mis
Ajax autosuggest autocomplete from database
Struts forms and validation with webflow
Java Apache POI Examples
JDBC Tutorial

Oracle
MYSQL Tutorial
php Tutorial
JQuery Examples
JQuery Tuorial
Upload Java File
JAXB Examples
PL-SQL Tutorial
Dojo AJAX
EJB Examples
Java IDE
Eclipse
NetBeans
IBM Rational Weblogic Workshop
Downloads
SCJP 1.6 Dumps
SCJP 1.5 DUMP
SCJP Material
SCWCD
Java Security
SCBCD Dumps
External Links
SUN Java
JOracle
Bea Weblogic
IBM
PHP Certification
Technical Interview Questions
Amazon Interview Questions
Google Interview Questions
Microsoft Interview Questions
IBM Interview Questions
Yahoo Interview Questions
HOME | FORUM | ABOUT | BLOGS | CONTACT-US