2015-09-07 11:44:36 -06:00
|
|
|
|
// hiding/Lunch.java
|
2015-11-14 16:18:05 -08:00
|
|
|
|
// <20>2016 MindView LLC: see Copyright.txt
|
2015-11-15 15:51:35 -08:00
|
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
|
|
|
|
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
|
2015-06-15 17:47:35 -07:00
|
|
|
|
// Demonstrates class access specifiers. Make a class
|
|
|
|
|
// effectively private with private constructors:
|
|
|
|
|
|
|
|
|
|
class Soup1 {
|
|
|
|
|
private Soup1() {}
|
2015-12-02 09:20:27 -08:00
|
|
|
|
public static Soup1 makeSoup() { // (1)
|
2015-06-15 17:47:35 -07:00
|
|
|
|
return new Soup1();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Soup2 {
|
|
|
|
|
private Soup2() {}
|
2015-12-02 09:20:27 -08:00
|
|
|
|
private static Soup2 ps1 = new Soup2(); // (2)
|
2015-06-15 17:47:35 -07:00
|
|
|
|
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();
|
|
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
|
}
|