class A{
int y=6;
class Inner{
static int y=3;
void show(){
System.out.println(y);
}
}
}
class Demo{
public static void main(String[] args) {
A.Inner inner=new A().new Inner();
inner.show();
}
}
class A{
public A(){
System.out.println("A");
}
}
class B extends A {
public B(){
System.out.println("B");
}
public static void main(String[] args) {
B b=new B();
}
}
第一个程序编译出错,static不能修饰变量。
第二个程序输出:“A” “B”;
因为实例化子类时优先实例化父类对象,先有父类对象才有子类对象。
先调用父类的构造方法后在调用子类构造。