• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

private constructor

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does it means to have a private constructor.
In k & B its written that-
A private constructor means only code within the class itself can instantiate an object of that type, so if the private constructor class wants to allow an instance of the class to be used, the class must provide a static method or variable that allows access to an instance created from within the class.

This is not clear.
Thanks in advance.
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Keeping a constructor private would mean that no code can create an instance of that class using the new operator i.e.

Will not be allowed.
One use of this is in the Singleton pattern wherein it is required to create only one instance of the class. To read more about the singleton pattern check this link

HTH
Shikhar
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can only create an instance within a method of the class
Example:
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shikhar Madhok:
Keeping a constructor private would mean that no code can create an instance of that class using the new operator i.e.

Will not be allowed.
One use of this is in the Singleton pattern wherein it is required to create only one instance of the class. To read more about the singleton pattern check this link

HTH
Shikhar[/qb]



No.
Only code within the class that has the private constructor can create and instance of the class using the new keyword.
Code from other classes cannot invoke the new keyword on an instance of the class that has the private constructor.
You could create a static instance within your class and have a static factory method that returns a reference to this singleton object like so:

Best regards

[ July 13, 2008: Message edited by: Keith Nagle ]
[ July 13, 2008: Message edited by: Keith Nagle ]
 
rakhee gupta
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok that means a class with private constructor cannot be instantiated from outside the class.But what is the use of having a private constructor.What purpose does it serves?
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://en.wikipedia.org/wiki/Singleton_pattern
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you want to regulate/govern the instantiation of your class,you make the constructor private or protected.

It can be used to achieve singleton or factory pattern.lets have a look on two cases:

1.Constructor is Private::

In this case it will restrict the inheritance and instantiation of this class by any outside class.But Still you can provide the public static method
to get the instance(any no of instance) of this class.To make this singleton you need to write your own piece of code.

Example of Singleton with private constructor

public class ClassName{
private static ClassName instance = null;

private ClassName(){
//To defeat instantiation and inheritance.
};

public static ClassName getInstance() {

/****To make the class follow singleTon*********/
if(instance == null) {
instance = new ClassName();
}
return instance;

}


2.Constructor is Protected::

In this case it will restrict the instantiation of this class for out side classes.But at the same time protected constructor follow the rule
of protected access modifier i.e. any class in same package can instantiate this class as they can access the protected constructor as per rule.

public class ClassName{
private static ClassName instance = null;

protected ClassName(){
//To defeat instantiation only.Still it can be inherited.

};

public static ClassName getInstance() {
instance = new ClassName();
return instance;
}
}
 
You showed up just in time for the waffles! And 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
reply
    Bookmark Topic Watch Topic
  • New Topic