samedi 28 mars 2015

static block and static varibles in java




class Person{
public static String name="Mr.zhang";
//public static int age;
static{
age=20;
System.out.println("initiate age");
}
public static int age;
public static String address;
static{
address="beijing";
age=34;
}
public static void main(String[] args){
System.out.println(name);
System.out.println(age);
System.out.println(address);
}


I define the variable——"age" after its initiation and I find that it works.Both of them are static so why they are not executed according to the sequence of code.




Aucun commentaire:

Enregistrer un commentaire