class access example
阿新 • • 發佈:2018-12-27
// hiding/Lunch.java
// (c)2017 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();
System.out.println("soup:" + soup);
System.out.println("Soup1.makeSoup():" + Soup1.makeSoup());
}
void testSingleton() {
Soup2.access().f();
}
public static void main(String[] args) {
new Lunch().testStatic();
}
}
/* My Output:
soup: [email protected]
Soup1.makeSoup():[email protected]
*/
summary, there's one and only one Soup2, Soup1 has multiple instances.
references:
1. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/hiding/Lunch.java
2. On Java 8 - Bruce Eckel