Re: Types of constructors



A private constructor is a constructor for a class that no one outside of
the class (and it's descendents) can call.
Useful if you don't want anyone to create an object of the class type (other
than a descendent or the code in a static method).

In simply factory method pattern, you may place the factory method in the
class that is being created. The factory method would call the private
constructor.
Singleton is often implemented this way as well.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Sathyaish" <Sathyaish@xxxxxxxxx> wrote in message
news:1116791071.111938.267910@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> What is a private constructor, and why would a class have one? What are
> the other kinds of constructors besides:
>
> (1) public constructors; and
> (2) parameterized constructors
>
> And I understand that they are not mutually exclusive of one another.
> The above classification assimilates my knowledge of having used
> constructors in both the above manners.
>


.



Relevant Pages

  • Re: Why can I not use: Math a=new Math();
    ... > on just not declaring a constructor. ... > declaring a public constructor that throws an exception. ... cannot be instantiated (by using private constructor). ... Namespace would just be ...
    (comp.lang.java.programmer)
  • Re: Is there a way to disallow subclassing
    ... It can be done by using virtual inheritance: ... // class with private constructor and destructor ... class NotToBeSubclassed: virtual Locker ... a most-derived class must call the constructor ...
    (comp.lang.cpp)
  • Re: Types of constructors
    ... I private constructor is a constructor that is only available to the same class. ... public class Foo { ... You can also have static constructors, which can be used to initialize static fields of the class. ...
    (microsoft.public.dotnet.general)
  • Re: Why can I not use: Math a=new Math();
    ... > use test coverage tools and they report that default constructor as not ... > test to check if it has the default constructor when there is no purpose to ... If I switch to a private constructor the ... Tools that document interfaces will not ...
    (comp.lang.java.programmer)
  • Re: My LogGenerator.java wont compile - too many errors - help
    ... I meant static factory method, it's what you implemented to replace the ... constructor that you made private. ... Consider the singleton pattern. ...
    (comp.lang.java.help)