Class Access
Note that a class cannot be private (that would make it inaccessible to anyone but the class) or protected. So you have only two choices for class access: package access or public. If you don’t want anyone else to have access to that class, you can make all the constructors private, thereby preventing anyone but you, inside a static member of the class, from creating an object of that class. Here’s an example:
//: access/Lunch.java // Demonstrates class access specifiers. Make a class // effectively private with private constructors: class Soup1 { private Soup1() {} // (1) Allow creation via static method: public static Soup1 makeSoup() { return new Soup1(); } } class Soup2 { private Soup2() {} // (2) Create a static object and return a reference // upon request.(The "Singleton" pattern): private static Soup2 ps1 = new Soup2(); public static Soup2 access() { return ps1; } public void f() {} } // Only one public class allowed per file: public class Lunch { void testPrivate() { // Can't do this! Private constructor: //! Soup1 soup = new Soup1(); } void testStatic() { Soup1 soup = Soup1.makeSoup(); } void testSingleton() { Soup2.access().f(); } } ///:~
[Thinking in Java, 160]