• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Private Constructor? Why I would want one?

 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
     
    Ranch Hand
    Posts: 5575
    Eclipse IDE Windows XP Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    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.
     
    Bartender
    Posts: 2700
    IntelliJ IDE Opera
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Bartender
    Posts: 6663
    5
    MyEclipse IDE Firefox Browser Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    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.
     
    André Asantos
    Ranch Hand
    Posts: 234
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    I really loved all helps...

    André AS
    São Paulo - Brazil
     
    To avoid criticism do nothing, say nothing, be nothing. -Elbert Hubbard. Please critique this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
      Bookmark Topic Watch Topic
    • New Topic