The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Private Constructor? Why I would want one? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Private Constructor? Why I would want one?" Watch "Private Constructor? Why I would want one?" New topic
Author

Private Constructor? Why I would want one?

André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 233
I know there are three ways to declare a Java constructor, such as public, protected and private, but why I would want private one?

Thanks in advance...

André AS
São Paulo - Brazil


André AS
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

André Asantos wrote:I know there are three ways to declare a Java constructor, such as public, protected and private, but why I would want private one?

Thanks in advance...

André AS
São Paulo - Brazil


when we don't want to create object from out side of the class, in this way we can provide security to our class.
There could be several situations when you want to make a constructor privates such as:
  • class contains static utility methods only.
  • A singleton behavior required.



  • Hope this helps

    Minhaj

    PS: take a look here for singleton details

    This message was edited 1 time. Last update was at by Minhaj Mehmood



    SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
    Seetharaman Venkatasamy
    Bartender

    Joined: Jan 28, 2008
    Posts: 4057

    André Asantos wrote:but why I would want private one?

    When you dont want to create an instance of a class using its that particular constructor.


    fall down seven times, get up eight times-bodhidharman
    Wouter Oet
    Saloon Keeper

    Joined: Oct 25, 2008
    Posts: 2700

    A side effect of making (all) the constructors private is that the class can't be sub-classed. This is because the constructor in the sub-class can't "see" a super class constructor.


    "Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
    Please correct my English.
    Deepak Bala
    Bartender

    Joined: Feb 24, 2006
    Posts: 6322

    André Asantos wrote:I know there are three ways to declare a Java constructor, such as public, protected and private, but why I would want private one?

    Thanks in advance...

    André AS
    São Paulo - Brazil


    One more example is a 'Constants' class. I usually use a Constants class to maintain public static final constants that will not change. I do not want this class to be instantiated since it would not make sense sub classing this class.


    SCJP 6 articles - SCJP 5/6 mock exams - SCJP Mocks - SCJP 5 Mock exam (Word document ) - SCJP 5 Mock exam in Java.Inquisition format
    André Asantos
    Ranch Hand

    Joined: Nov 23, 2009
    Posts: 233

    I really loved all helps...

    André AS
    São Paulo - Brazil
     
     
    subject: Private Constructor? Why I would want one?
     
    Two Laptop Bag