public class StaticAndInnerClasses {
String testString;
class InnerClass{
int x;
int y;
}
static class InnerStaticClass{
int a=10;
int b=20;
}
public void changeCharacter(){
testString="Characters modified";
}
public static void main(String[] args){
StaticAndInnerClasses.InnerStaticClass statciClassObject=new StaticAndInnerClasses.InnerStaticClass();
StaticAndInnerClasses outerObject=new StaticAndInnerClasses();
outerObject.changeCharacter();
InnerClass innerClassObject=outerObject.new InnerClass();
innerClassObject.changeCharacter();
}
}
As you can see in my code that I have created a innerClassObject in the last line, now I want to know why I am not able to access the String testString using innerClassObject. isn't testString a Global variable and haven't I created the instance of innerClass within the outerClass .i.e "StaticAndInnerClasses" And if it is not possible then why have the Inner Classes
正如您在我的代码中看到的,我在最后一行中创建了一个innerClassObject,现在我想知道为什么我不能使用innerClassObject访问字符串testString。testString不是全局变量,我没有在outerClass中创建innerClass的实例。如果不可能的话,为什么要有内部类。
2 个解决方案
#1
1
Code inside the inner class can reference the stuff in the outer class.
内部类中的代码可以引用外部类中的内容。
For this toy example:
这个玩具的例子:
public class StaticAndInnerClasses {
String testString;
class InnerClass{
int x;
int y;
public void printString() {
changeCharacter();
System.out.println(testString);
}
}
static class InnerStaticClass{
int a=10;
int b=20;
}
public void changeCharacter(){
testString="Characters modified";
}
public static void main(String[] args){
StaticAndInnerClasses.InnerStaticClass statciClassObject=new StaticAndInnerClasses.InnerStaticClass();
StaticAndInnerClasses outerObject=new StaticAndInnerClasses();
StaticAndInnerClasses.InnerClass innerClassObject=outerObject.new InnerClass();
innerClassObject.printString();
}
}
So in the code of the inner class, you can reference methods and fields of the outer class, as is done here in the method printString()
. This does not turn the outer class methods into methods of the inner class so the code (in the outer class main
)
因此,在内部类的代码中,可以引用外部类的方法和字段,就像在方法printString()中所做的那样。这并没有将外部类方法转换为内部类的方法,因此代码(在外部类main中)
innerClassObject.changeCharacter();
as you referenced in a comment, will not work. It in fact won't compile.
正如您在评论中所引用的,将不起作用。它实际上不会编译。
#2
1
There's a difference between an inner class and a static inner class.
内部类和静态内部类之间有区别。
A static inner class is not associated with any instance of the outer class, hence why you can't access the enclosing classes' fields.
静态内部类与外部类的任何实例都没有关联,因此为什么不能访问封闭类的字段。
However, a non-static inner class must be constructed using an instance of the outer class. So, your test code would look like:
但是,必须使用外部类的实例构造非静态的内部类。因此,您的测试代码如下所示:
OuterClass outer = new OuterClass();
InnerClass inner = outer.new InnerClass();//Yes, with that exact syntax!
EDIT For those saying the above does not compile, here's the exact message from Eclipse when trying to instantiate InnerClass
directly:
编辑那些说上面没有编译的内容,这里是在尝试直接实例化InnerClass时从Eclipse得到的确切消息:
No enclosing instance of type OuterClass is accessible. Must qualify the allocation with an enclosing instance of type OuterClass(e.g. x.new A() where x is an instance of OuterClass).
没有可访问类型OuterClass的封闭实例。必须用类的封闭实例来限定分配(例如:x。新A(),其中x是一个OuterClass实例。
#1
1
Code inside the inner class can reference the stuff in the outer class.
内部类中的代码可以引用外部类中的内容。
For this toy example:
这个玩具的例子:
public class StaticAndInnerClasses {
String testString;
class InnerClass{
int x;
int y;
public void printString() {
changeCharacter();
System.out.println(testString);
}
}
static class InnerStaticClass{
int a=10;
int b=20;
}
public void changeCharacter(){
testString="Characters modified";
}
public static void main(String[] args){
StaticAndInnerClasses.InnerStaticClass statciClassObject=new StaticAndInnerClasses.InnerStaticClass();
StaticAndInnerClasses outerObject=new StaticAndInnerClasses();
StaticAndInnerClasses.InnerClass innerClassObject=outerObject.new InnerClass();
innerClassObject.printString();
}
}
So in the code of the inner class, you can reference methods and fields of the outer class, as is done here in the method printString()
. This does not turn the outer class methods into methods of the inner class so the code (in the outer class main
)
因此,在内部类的代码中,可以引用外部类的方法和字段,就像在方法printString()中所做的那样。这并没有将外部类方法转换为内部类的方法,因此代码(在外部类main中)
innerClassObject.changeCharacter();
as you referenced in a comment, will not work. It in fact won't compile.
正如您在评论中所引用的,将不起作用。它实际上不会编译。
#2
1
There's a difference between an inner class and a static inner class.
内部类和静态内部类之间有区别。
A static inner class is not associated with any instance of the outer class, hence why you can't access the enclosing classes' fields.
静态内部类与外部类的任何实例都没有关联,因此为什么不能访问封闭类的字段。
However, a non-static inner class must be constructed using an instance of the outer class. So, your test code would look like:
但是,必须使用外部类的实例构造非静态的内部类。因此,您的测试代码如下所示:
OuterClass outer = new OuterClass();
InnerClass inner = outer.new InnerClass();//Yes, with that exact syntax!
EDIT For those saying the above does not compile, here's the exact message from Eclipse when trying to instantiate InnerClass
directly:
编辑那些说上面没有编译的内容,这里是在尝试直接实例化InnerClass时从Eclipse得到的确切消息:
No enclosing instance of type OuterClass is accessible. Must qualify the allocation with an enclosing instance of type OuterClass(e.g. x.new A() where x is an instance of OuterClass).
没有可访问类型OuterClass的封闭实例。必须用类的封闭实例来限定分配(例如:x。新A(),其中x是一个OuterClass实例。