You 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 [at] java4s.com
Follow me on


Comments

11 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?

  10. thasil says:

    @loshith kumar

    we should not implement cloneable

    @java4s

    we should use synchronized in object creation section not in method

  11. Anuradha says:

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

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
Sitemap :
spring Hibernate struts Json Ajax Log4j Log4j coreJava Servlets Web Services


Upcoming
  Web Services [ SOAP ], JSP
Facebook_Java4s  Twitter_Java4s  GooglePlus_Java4s  Youtube_Java4s 
© 2010 - 2014 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.