简体   繁体   English

如何使用外部类名称调用非静态内部类

[英]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 : 下面的内部类InnerOuter类的成员:

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.

相关问题 如何使用外部非静态类的对象访问静态内部类方法? - How can I access static inner class method using object of outer non static class? 如何从不使用外部类名的内部类中获取外部类的引用? - How can one get reference to the outer class from inner class not using outer class name? 如何使用内部静态类对象访问外部类变量 - How to access outer class variable using inner static class object 为什么这个静态内部类不能在其外部类上调用非静态方法? - Why can’t this static inner class call a non-static method on its outer class? 静态内部类如何访问外部类的所有静态数据成员和静态成员函数? - How does static inner class can access all the static data members and static member function of outer class? 可以将外部类定义为静态并包含内部静态类吗? - Can outer class be defined as static and enclose inner static class? 如何在非静态内部类的另一个实例中引用外部类? - How to refer to the outer class in another instance of a non-static inner class? 外部和内部类以及静态方法 - Outer and Inner class and static methods 静态内部类可以扩展其外部类吗? - Can a static inner class extend its outer class? 为什么我们可以使用'new'关键字在非静态外部类中获取静态内部类的引用? - Why can we use 'new' keyword to get reference of a static inner class within a non-static outer class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM