Search This Blog

Saturday, May 19, 2007

Where and how can you use a private constructor?

Private constructor is used if you do not want other classes to instantiate the object. The instantiation is done by a public static method within the same class.1. Used in the singleton pattern. (Refer Q45 in Java section).2. Used in the factory method pattern (Refer Q46 in Java section).3. Used in utility classes e.g. StringUtils etc.

Why there are some interfaces with no defined methods (i.e. marker interfaces) in Java?

The interfaces with no defined methods act like markers. They just tell the compiler that the objects of the classes implementing the interfaces with no defined methods need to be treated differently. Example Serializable, Cloneable etc

The code in a finally clause will never fail to execute, right? Well, hardly ever. But here's an example where the finally code will not execute, regardless of the value of the boolean choice: