Saturday, December 23, 2017

JAVA - How disable instantiate of class

In some situation class works for example as storage for static methods. In this situation is useful to disable instantiating of class.

The trick is easy - only mark constructor of the class a private.
public class Person {

  private Person() {}

  String name;
The compiler shows this message (NetBeans):

No comments:

Post a Comment