40 lines
743 B
Java
40 lines
743 B
Java
//: initialization/ExplicitStatic.java
|
|
// ©2015 MindView LLC: see Copyright.txt
|
|
// Explicit static initialization with the "static" clause.
|
|
import static net.mindview.util.Print.*;
|
|
|
|
class Cup {
|
|
Cup(int marker) {
|
|
print("Cup(" + marker + ")");
|
|
}
|
|
void f(int marker) {
|
|
print("f(" + marker + ")");
|
|
}
|
|
}
|
|
|
|
class Cups {
|
|
static Cup cup1;
|
|
static Cup cup2;
|
|
static {
|
|
cup1 = new Cup(1);
|
|
cup2 = new Cup(2);
|
|
}
|
|
Cups() {
|
|
print("Cups()");
|
|
}
|
|
}
|
|
|
|
public class ExplicitStatic {
|
|
public static void main(String[] args) {
|
|
print("Inside main()");
|
|
Cups.cup1.f(99); // (1)
|
|
}
|
|
// static Cups cups1 = new Cups(); // (2)
|
|
// static Cups cups2 = new Cups(); // (2)
|
|
} /* Output:
|
|
Inside main()
|
|
Cup(1)
|
|
Cup(2)
|
|
f(99)
|
|
*///:~
|