public class BedAndBreakfast { // initialize to 10 public static int capacity = 10; // initialize to false private boolean full = false; }
static { // whatever code is needed for initialization goes here }
{ // whatever code is needed for initialization goes here }
4. constructor
5. example
public class t1 { { System.out.println("Empty block"); } static { System.out.println("Static block"); } public t1() { System.out.println("constructor block"); } public static void main(String[] args) { t1 t = new t1(); } }
http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html Initializing Fields
http://www.javatpoint.com/instance-initializer-block Instance initializer block:
原文:http://www.cnblogs.com/phoenix13suns/p/3893629.html