目的是初始化和便于阅读
静态字句或静态块格式为:
staticint i;
staticString s;
static{
i = 47;
s = " is a magic number!";
}
非静态实例初始化格式:
Cupcup3;
Cupcup4;
{
cup3 = new Cup(3);
cup4 = new Cup(4);
}
*
*静态字句或静态块和非静态实例初始化
*/
public class ExplicitStatic {
static int i;
static String s;
static {
i = 47;
s = " is a magic number!";
}
public static void main(String[] args) {
System.out.println(i+s);
System.out.println("Inside main()");
Cups.cup2.f(47);
new Cups();
}
}
class Cup {
Cup(int marker) {
System.out.println("Cup(" + marker + ")");
}
void f(int marker) {
System.out.println("f(" + marker + ")");
}
}
class Cups {
static Cup cup1;
static Cup cup2;
static {
cup1 = new Cup(1);
cup2 = new Cup(2);
}
Cup cup3;
Cup cup4;
{
cup3 = new Cup(3);
cup4 = new Cup(4);
}
Cups() {
System.out.println("Cups()");
}
}