37 lines
852 B
Java
37 lines
852 B
Java
// hiding/Lunch.java
|
|
// (c)2021 MindView LLC: see Copyright.txt
|
|
// We make no guarantees that this code is fit for any purpose.
|
|
// Visit http://OnJava8.com for more book information.
|
|
// Demonstrates class access specifiers. Make a class
|
|
// effectively private with private constructors:
|
|
|
|
class Soup1 {
|
|
private Soup1() {}
|
|
public static Soup1 makeSoup() { // [1]
|
|
return new Soup1();
|
|
}
|
|
}
|
|
|
|
class Soup2 {
|
|
private Soup2() {}
|
|
private static Soup2 ps1 = new Soup2(); // [2]
|
|
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();
|
|
}
|
|
}
|