File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes public private constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "public private constructor" Watch "public private constructor" New topic
Author

public private constructor

varungoyal goyal
Ranch Hand

Joined: May 31, 2006
Posts: 37
if a constructor can be public or private ,,,what is the reason..and where is it needed to make them public or private ...please examplify..thanks
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
You can make a constructor private to prevent instances of the class from being made outside the class.

You make a constructor public to allow instances of the class to be created anywhere the class can be seen.
varungoyal goyal
Ranch Hand

Joined: May 31, 2006
Posts: 37
then it can also be done by specifying access specifier to clas like making a class public and private then why making constructor private and public comes into picture
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
You can have private methods in a public class, because you want to restrict access to those methods.
Similarly you can have private constructors to prevent them being used outside the class, even when there are other public methods.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Because there are situations where you want the class to be visible, but not allow the user to create more than once instance of the class, like in the singleton pattern.

There are different situations where you might want instance variables, constructors or methods to have certain access control even if the class is public.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You are probably most familiar with a public class with a public constructor. Any time you use new Something() from some other class you are invoking a public constructor.

If the class has a private constructor, then new Something() from another class will not compile. This is useful when you want to control who can create instances of some class. We can use the Singleton Pattern when we want to have exactly one instance of some class. It's often implemented like this:

Does that help?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
varungoyal goyal
Ranch Hand

Joined: May 31, 2006
Posts: 37
so you mean to say if we have a class public but constructor as a private then if that class instance is created more than once then it will give error..right ??
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by varungoyal goyal:
so you mean to say if we have a class public but constructor as a private then if that class instance is created more than once then it will give error..right ??


No. If there's a class with a private constructor, then that class can construct instances of itself, but no other code can. That class can then choose to construct only one instance, and not provide any way for any other class to create any other instances.

For an example, look at the java.lang.Runtime class. You can get an instance by calling the static method Runtime.getRuntime(), but can you construct your own separate instance? Go ahead and try!


[Jess in Action][AskingGoodQuestions]
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

Just to clarify, this statement only applies to top-level classes (those that are not declared within an enclosing type).


Tony Morris
Java Q&A (FAQ, Trivia)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: public private constructor