Get all updates of Java4s
Follow Us: Email Newsletter

How to Create Singleton Class in Java, Singleton Class in Java

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



You Might Also Like

  ♦ About Author ♦

Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. Sign-up for the Email Newsletter for your daily dose of Java tutorials.


21 Responses to “How to Create Singleton Class in Java, Singleton Class in Java”
  1. Pavan says:

    Sir,please can u explain the program?

    • Naveen Gupta says:

      I am trying the singleton class example but it is given error when we creating constructor as private. can u explain about that.


  2. suresh says:

    You should provide private constructor to “AccountCreation” class. Otherwise user can create new instance for that.

  3. Srinivasa says:


    I Agree with you..

  4. the above class should implement cloneable interface otherwise we can clone object i.e we can create duplicate object for class

  5. What is difference between enumration and abstract class

  6. Bhushan says:

    Hi Sir,

    Please provide some explanations of Design Patterns also.

  7. satya says:

    @Suresh. I agree with you. We need to create private constructor.

  8. Arshi says:

    how to prevent cloning of singleton class?

  9. thasil says:

    @loshith kumar

    we should not implement cloneable


    we should use synchronized in object creation section not in method

  10. Anuradha says:

    plz tell me some concepts of creating writing reading from file in java

  11. Satish says:

    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.

  12. pavan says:

    sir, please add few more core java real time scenarios like these. Awesome site sir.

  13. Ramesh says:

    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………

  14. Renjith says:

    Can you give an example for multiple object creation for any java program?
    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.

  15. madhu says:

    Hi Sir
    I know Factory DesignPattern but in real time why you design in HealthCare and Banking domain

  16. Rambabu says:


    While in de-serialization of the above AccountCreation instance , every time we will get new object. How to avoid it?

  17. Anil says:

    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

  18. pullarao says:

    Can You Please Give me Some Other Example without using Synchronization Sir

  19. rijesh says:

    Can You Please Give me Some Example for ip address validation sir???


By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful LinksCurrent & UpComing Tutorials
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2017 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.