| Author |
creating no object for a class
|
|
|
| i got a query that,
the programmer should not be able to create any object of a particular
calss. the only way to create an object is calling amethod.
how to do this?
| |
| Ian Wilson 2007-01-17, 8:12 am |
| soms wrote:
> i got a query that,
>
> the programmer should not be able to create any object of a particular
> calss. the only way to create an object is calling amethod.
> how to do this?
>
Make the constructor private.
| |
| printdude1968@gmail.com 2007-01-23, 7:30 pm |
|
Ian Wilson wrote:
> Make the constructor private.
Just out of curiosity, why make the constructor private? I know that
encapsulation says to make the instance variables private and the
methods (getters and setters) public. But if you make a constructor
private, how would you create a new instance of the class? I suppose
you'd have to call another method that is public and can invoke the
private constructor? Isn't that a bit of overkill?
| |
| Steve W. Jackson 2007-01-23, 7:30 pm |
| In article <1169569828.340108.14490@a75g2000cwd.googlegroups.com>,
"printdude1968@gmail.com" <printdude1968@gmail.com> wrote:
> Ian Wilson wrote:
>
>
> Just out of curiosity, why make the constructor private? I know that
> encapsulation says to make the instance variables private and the
> methods (getters and setters) public. But if you make a constructor
> private, how would you create a new instance of the class? I suppose
> you'd have to call another method that is public and can invoke the
> private constructor? Isn't that a bit of overkill?
That answer was in reply to a question on how to prevent someone from
creating an instance of a class. Making any and all constructors
private is the only way to guarantee that no code outside the class
itself can create an instance.
This is typically done when using the singleton design pattern.
--
Steve W. Jackson
Montgomery, Alabama
| |
| printdude1968@gmail.com 2007-01-23, 7:30 pm |
|
> This is typically done when using the singleton design pattern.
> --
Never heard of the singleton before. I googled it and found this site:
http://www.inquiry.com/techtips/cpp...n/10min0200.asp
It introduces the idea of pointers in Java. I thought that pointers
did not exist in Java.
| |
|
|
|
| printdude1968@gmail.com wrote:
RedGrittyBrick wrote:[color=darkred]
> That is an article on C++ not Java. OO design patterns like singleton
> apply to many OO languages.
>
> http://en.wikipedia.org/wiki/Singleton_pattern#Java
Pointers do exist in Java. Java calls them "variables".
- Lew
| |
|
| Ian Wilson wrote:
"printdude1968 wrote:[color=darkred]
It is not overkill. Joshua Bloch treats this topic in detail in _Effective
Java_. There are legitimate reasons for a private constructor; one of them
went away with the addition of "enum" to Java: the construction of fixed
values of a class where no client can create new ones.
Steve W. Jackson wrote:[color=darkred]
> This is typically done when using the singleton design pattern.
Or a factory pattern, or a (hand-rolled) type-safe enum, or a class to hold
only static methods, or ...
- Lew
| |
| Tor Iver Wilhelmsen 2007-01-25, 7:09 pm |
| Lew <lew@nowhere.com> writes:
> or a class to hold only static methods, or ...
Aka. the Utility pattern.
|
|
|
|