简体   繁体   English

为什么我在方法本地Inner类中没有访问说明符?

[英]Why can't i have an access specifier in a method local Inner class?

Why Method Local Inner class cannot be public? 为什么方法本地内部类不能公开? I know it is useless to make them private but why not public? 我知道将它们私有化是没用的,但为什么不公开呢? Is that because Method Local Inner is also treated as local variables? 那是因为Method Local Inner也被视为局部变量吗? As local variables cannot be public. 由于局部变量不能公开。 Am i going right or missing something? 我是对的还是错过了什么?

public class OuterClass {
   public void outerMethod() {
   public class Inner {  // having problem here
    ... 
    }
  }
 }

Since it is declared within the method body, ie. 因为它是在方法体内声明的,即。 it's a local class, you can't refer to it by name anywhere else in your code, only in the method body itself following its declaration. 它是一个本地类,你不能在代码中的任何其他位置引用它,只能在声明后的方法体本身中引用它。 It is therefore pointless for it to have an access modifier. 因此,它具有访问修饰符是没有意义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我不能在声明方法之外访问本地类? - Why can't I access local class outside declaring method? 如何从内部类方法访问局部变量? - How can I access to local variable from inner class method? 为什么扩展内部类的本地类不能访问内部类封闭实例? - Why can't a local class that extends an inner class access the inner class enclosing instance? 覆盖方法是否可以使用与基类中不同的访问说明符? - Can an overriding method have a different access specifier from that in the base class? 为什么不能在匿名内部类中创建新方法? - Why can't I create a new method in an anonymous inner class? 如何通过另一种方法访问本地内部类的字段? - How can I access a field of a local-inner class from another method? 为什么我们不能在方法中声明私有本地内部类? - Why can't we declare private local inner class inside a method? 为什么我们不能在(非静态)内部类(Java 16 之前)中使用静态方法? - Why can't we have static method in a (non-static) inner class (pre-Java 16)? 为什么我不能有一个与其包含类同名的两级深度内部类? - Why can't I have a two-level-deep inner class with the same name as its containing class? 如何访问与其他类的Method Local Inner类同名的类 - How to access a class which have the same name as the Method Local Inner class of a different class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM