Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Singleton pattern: the purpose of empty constructor

  1. #1
    Member
    Join Date
    Jun 2010
    Posts
    48
    Thanks
    12
    Thanked 2 Times in 2 Posts

    Default Singleton pattern: the purpose of empty constructor

    Hello,
    So, as the topic says, I have noticed that people use empty constructor when implementing singleton pattern. I am wondering what is the difference between empty constructor and the default constructor, which is called if the class has no constructor at all?


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Singleton pattern: the purpose of empty constructor

    If you are using the singleton pattern, you want to ensure that no one can create another instance of that class.

    For this, you need to make sure that all your constructors are declared private. However, since you can only ever have one object, it makes no sense to provide "specialized constructors" other than an empty constructor because the values will always be initialized to the same default setting, so you might as well hard code them in.

    You can't use the default constructor because it has public access, violating the Singleton Pattern.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    Asido (August 6th, 2010)

Similar Threads

  1. a new pattern with a new architecture
    By mcgrow in forum Web Frameworks
    Replies: 0
    Last Post: August 4th, 2010, 02:28 PM
  2. Problem with updating empty JTable
    By byubi in forum AWT / Java Swing
    Replies: 1
    Last Post: May 15th, 2010, 02:06 AM
  3. How to show empty directories in JTree ?
    By ni4ni in forum AWT / Java Swing
    Replies: 1
    Last Post: April 30th, 2010, 12:55 AM
  4. [SOLVED] Supressing empty lines and does not do until the end! Why ?
    By lumpy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 18th, 2010, 07:38 AM
  5. url pattern issue - please help me...
    By bharathik in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: November 9th, 2009, 04:28 AM