Can view the tutorials best in Google Chrome, Mozilla Firefox, Opera, higher version of Internet Explorer

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

Core Java » On Sep 29, 2012 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

Client.java

Output







By [ Java/J2EE Developer & Web Designer ]
Founder of Java4s.com - Indian, lives in United States. You can contact me at sivateja@java4s.com


Comments

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

    Sir,please can u explain the AccountCreation.java program?
    thanks..,

  2. suresh says:

    Hi,
    You should provide private constructor to “AccountCreation” class. Otherwise user can create new instance for that.
    Thanks,
    Suresh.p

  3. Java4s says:

    @Suresh

    I Agree :-)

  4. Srinivasa says:

    @Suresh

    I Agree with you..

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

  6. Ishwar Kumar says:

    What is difference between enumration and abstract class

  7. Bhushan says:

    Hi Sir,

    Please provide some explanations of Design Patterns also.

  8. satya says:

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

  9. Arshi says:

    how to prevent cloning of singleton class?

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Tutorials
Hibernate Recent Posts
Spring Recent Posts
Struts Recent Posts
Recomandded Links Current & UpComing Tutorials Java4s.com
Tutorials Online :
spring Hibernate struts Json Ajax Log4j Log4j
coreJava Servlets


UpComing :
JSP, Web Services [ In Progress ]
Java4s is optimized for learning java technologies, all the examples in this site are constantly reviewed to avoid errors. While using this site you agree to have read and accepted our terms of use and privacy policy
Especially i have prepared this blog by keeping fresher's in mind, however it will be very useful for real time developers too.


© 2014 Java4s All rights reserved. | strPro4Tut v(2.0) Theme designed by Team Java4s.