how-to

Classes

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 very easy - only mark constructor of the class a private.

public class Person {

  private Person() {}

  String name;
}

The compiler shows this message (NetBeans):