[英]How non static inner class can be called using outer class name
public class InnerTest {
public static void main(String arg[]) {
A.B.print();
}
}
class A {
static class B {
static void print() {
System.out.println("Hello");
}
}
}
How can i call static class B using class name A although class A is not static 我如何使用类名A调用静态B类,尽管A类不是静态的
This is not related to the class to be static or not, it is related the static keyword in the method . 这与是否为静态类无关,与方法中的static关键字相关。
take a look about How static keyword exactly works in Java? 看一下static关键字在Java中到底如何工作? also read this article Java – Static Class, Block, Methods and Variables
也阅读了这篇文章Java –静态类,块,方法和变量
One more aspect how to explain this: 另一个方面是如何解释这一点的:
class
itself is not static
or non static
it is just a class
. class
本身不是static
non static
它只是一个class
。
You anyway can use static
keyword only with class
members. 无论如何,您只能对
class
成员使用static
关键字。 If you would try to declare InnerTest
as static
you would have an error that might look like this (so assuming it is not static nested class to some other class) 如果您尝试将
InnerTest
声明为static
,则将出现一个看起来像这样的错误(因此,假设它不是某个其他类的静态嵌套类)
Illegal modifier for the class InnerTest;
类InnerTest的非法修饰符; only public, abstract & final are permitted
只允许公开,摘要和最终作品
static
nested class
can be used as in question because it does not require access to any instance member of InnerTest
. static
嵌套class
可以InnerTest
使用,因为它不需要访问InnerTest
任何实例成员。 In other words it can access static
members and only those. 换句话说,它可以访问
static
成员,并且只能访问那些成员。
If it needs access to non static
members then it can not be static
and the way to call would be like new InnerTest().new B()
. 如果它需要访问
non static
成员,则它不能是static
并且调用方式将类似于new InnerTest().new B()
。
The static
keyword is used to modify a member of a class. static
关键字用于修改类的成员。 When a member is modified with static
, it can be accessed directly using the enclosing class' name, instead of using an instance of the enclosing class like you would with a non-static member. 如果使用
static
修改成员,则可以使用封闭类的名称直接对其进行访问,而不必像使用非静态成员那样使用封闭类的实例。
The inner class Inner
below is a member of the class Outer
: 下面的内部类
Inner
是Outer
类的成员:
class Outer {
static class Inner {}
}
Therefore, Inner
can be accessed like this: 因此,可以这样访问
Inner
:
Outer.Inner
Outer
don't need to/cannot be modified by static
because it is not a member of a class. Outer
不需要/不能通过static
修改,因为它不是类的成员。 It is a class existing in the global scope. 它是存在于全球范围内的一类。 To access it, you just write its name -
Outer
. 要访问它,只需输入其名称-
Outer
。 It does not make sense for it to be non-static because it has no enclosing class. 将其设为非静态是没有意义的,因为它没有封闭类。 If it were non-static, how are you supposed to access it?
如果它是非静态的,应该如何访问它?
To use the correct terminology, class B is not an inner class; 为了使用正确的术语,B类不是内部类; it is a static nested class .
它是一个静态的嵌套类 。 See https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html for the definitions of the various types of nested class.
有关各种类型的嵌套类的定义,请参见https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html 。
The important keyword is the static
keyword in front of the definition of class B. It does not matter whether class A is static or not. 重要的关键字是类B定义前面的
static
关键字。类A是否为静态都没有关系。 In fact, it wouldn't make sense to put static
in front of class A's definition. 实际上,将
static
放在类A的定义前面没有任何意义。
Because class B is declared static, it doesn't keep a reference to an instance of class A. 因为类B被声明为静态的,所以它不保留对类A实例的引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.