Constructors can use any access modifier, including private. (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.)
Can somebody explain this, if feasible demonstrate with a simple example...
Source: Sun certified programmer and developer exam
Where in for some reason you as the writer of the class do not want the user classes to instantiate objects of your class directly. Hence you ask the users of your class to call a static method defined within your class which in turn may do some validation and then may return a new object of your class or may return a reference to singleton.
Example code for plain factory pattern:
Example code for singleton factory pattern:
Factory pattern can be taken further to achieve the following: