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:
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.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
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.