Skip to content

Instantly share code, notes, and snippets.

@mckelvin
Created May 29, 2012 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mckelvin/2823153 to your computer and use it in GitHub Desktop.
Save mckelvin/2823153 to your computer and use it in GitHub Desktop.
Java静态代码块(static block)调用陷阱小记
//父类
public class Base {
public static String who = "Mr. Base";
Base(){
}
static {
System. out.println("static block in Base" );
}
public static void functionInBase(){
System. out.println("static function in Base! -- " + who);
}
public void showWho(){
System. out.print(who);
}
}
//子类
public class Extend extends Base{
public static String who = "Mr. Extend";
static {
System. out.println("static block in Extend" );
}
public static void functionInExtend(){
System. out.println("static function in Extend!" );
}
Extend(){
super();
}
}
//测试类
public class Main {
public static void main(String[] args) {
Extend.functionInBase();
Extend ex = new Extend();
ex.showWho();
System. out.println(Extend.who);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment