(非静态)普通内部类中不能有静态方法和静态变量 普通内部类的对象只能在与其外围类的对象相关的情况下才能被创建。构建普通内部类对象时,需要一个指向 其外围类对象的引用,即普通内部类只能通过外部类的对象才能创建。因此,普通内部类的字段与方法,只能 放在类的外部层次上,所以普通的内部类不能有static方法和static变量,也不能包含静态类。class A{ class B{ static void C(){}; //error: The method C cannot be declared static; // static methods can only be declared in a static or top level type
static int a; //error: The field a cannot be declared static; // static fields can only be declared in static or top level types } } 静态内部类一般称为 嵌套类,有如下性质: 1.要创建嵌套类的对象,并不需要其外围类的对象。 2.不能从嵌套类的对象中访问非静态的外围类对象。 嵌套类既可以包含static方法和变量,也可以包含嵌套类和普通内部类。
http://zhidao.baidu.com/question/124117953.html
比如这样
public class A{
static class B{
static int C;
}
}
普通内部类的对象只能在与其外围类的对象相关的情况下才能被创建。构建普通内部类对象时,需要一个指向
其外围类对象的引用,即普通内部类只能通过外部类的对象才能创建。因此,普通内部类的字段与方法,只能
放在类的外部层次上,所以普通的内部类不能有static方法和static变量,也不能包含静态类。class A{
class B{
static void C(){};
//error: The method C cannot be declared static;
// static methods can only be declared in a static or top level type
static int a;
//error: The field a cannot be declared static;
// static fields can only be declared in static or top level types
}
}
静态内部类一般称为 嵌套类,有如下性质:
1.要创建嵌套类的对象,并不需要其外围类的对象。
2.不能从嵌套类的对象中访问非静态的外围类对象。
嵌套类既可以包含static方法和变量,也可以包含嵌套类和普通内部类。