Newsletter |
How to Create Singleton Class in Java, Singleton Class in Java
Core Java » on Sep 29, 2012 { 21 Comments } By Sivateja
Making java class as singleton is very important in some real time projects, we may need to have exactly one instance of a class. Suppose in hibernateย SessionFactory should be singleton as its heavy weight, and some other Banking related projects mainly.
Some times creating singleton classes are very very important, if we are creating the object of some classes again and again ‘N’ number of times, that will definitely kills the memory and finally our application will get down, so we can avoid that with this concept.
Let us see how to..
Files required
- AccountCreation.java
- Client.java
AccountCreation.java
package single; public class AccountCreation { private static AccountCreation instance; private AccountCreation(){ //Private Constructor } public synchronized static AccountCreation getInstance() { if (instance==null) { instance = new AccountCreation(); System.out.println("AccountCreation Class Object creatred...!!!"); } else{ System.out.println("AccountCreation Class Object not Creatred just returned Created one...!!!"); } return instance; } public void create(int no) { System.out.println("Account Created Successfully, with Number:" +no); } }
Client.java
package single; public class Client { public static void main(String[] args) { AccountCreation tc = AccountCreation.getInstance(); AccountCreation tc1 = AccountCreation.getInstance(); tc.create(12345); tc1.create(67891); } }
Output
โ โโ
You Might Also Like
::. About the Author .:: | ||
Comments
21 Responses to “How to Create Singleton Class in Java, Singleton Class in Java”
Sir,please can u explain the AccountCreation.java program?
thanks..,
Hi,
I am trying the singleton class example but it is given error when we creating constructor as private. can u explain about that.
Thanks,
Naveen
Hi,
You should provide private constructor to “AccountCreation” class. Otherwise user can create new instance for that.
Thanks,
Suresh.p
@Suresh
I Agree ๐
@Suresh
I Agree with you..
the above class should implement cloneable interface otherwise we can clone object i.e we can create duplicate object for class
What is difference between enumration and abstract class
Hi Sir,
Please provide some explanations of Design Patterns also.
@Suresh. I agree with you. We need to create private constructor.
how to prevent cloning of singleton class?
@loshith kumar
we should not implement cloneable
@java4s
we should use synchronized in object creation section not in method
sir
plz tell me some concepts of creating writing reading from file in java
Sir , if we place this singleton class in to web project and deploye at same time then jvm well create two object for this singleton class how we can avoid thise.
sir, please add few more core java real time scenarios like these. Awesome site sir.
Sir,It is possible to create private static instance variables how it will can you explain sir private means we can use with in the block only static means we can use any where in the program how it will be possible can you explain sir please………
Can you give an example for multiple object creation for any java program?
Also
If my understanding is correct,to achieve singleton pattern synchronization is not required.
The same program we could write it without using the synchronized keyword.
Hi Sir
I know Factory DesignPattern but in real time why you design in HealthCare and Banking domain
Hi,
While in de-serialization of the above AccountCreation instance , every time we will get new object. How to avoid it?
1)in this program one problem is their,two threads try to create objs that time one obj wait until second obj release the lock.
if already obj created by second thread why again first thread take lock
Can You Please Give me Some Other Example without using Synchronization Sir
Can You Please Give me Some Example for ip address validation sir???