可以在 Java 中实例化静态嵌套类吗? [英] Can a static nested class be instantiated in Java?

查看:25
本文介绍了可以在 Java 中实例化静态嵌套类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从 Oracle 的 Java 教程我找到了这段文字:

From Oracle's Java tutorials I've found this text:

与类方法和变量一样,静态嵌套类与其外部类相关联.和静态类方法一样,静态嵌套类不能直接引用在其封闭类中定义的实例变量或方法——它只能通过对象引用来使用它们.

As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class — it can use them only through an object reference.

注意:静态嵌套类与其外部类(和其他类)的实例成员交互,就像任何其他顶级类一样.实际上,静态嵌套类在行为上是一个顶层类,为了方便打包,它嵌套在另一个顶层类中.

Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.

使用封闭类名访问静态嵌套类:

Static nested classes are accessed using the enclosing class name:

OuterClass.StaticNestedClass

OuterClass.StaticNestedClass

例如,要为静态嵌套类创建对象,请使用以下语法:

OuterClass.StaticNestedClassnestedObject =new OuterClass.StaticNestedClass();

我认为无法实例化静态类,所以我不太理解粗体的句子.

I thought it is not possible to instantiate a static class, so I don't really understand the sentence in bold.

你知道这是什么意思吗?

Do you have any idea what it means?

推荐答案

您要么像 kihero 所说的那样将 staticabstract 混淆,要么混淆了概念具有 static 方法的类(这只是一个碰巧具有静态方法的类).

You are either confusing static with abstract as kihero says, or you are muddling the concept with a class that has static methods (which is just a class that happens to have static methods).

静态嵌套类只是一个嵌套类,不需要其封闭类的实例.如果您熟悉 C++,那么 C++ 中的所有类都是静态"类.在 Java 中,嵌套类默认不是静态的(这种非静态变体也称为内部类"),这意味着它们需要外部类的实例,它们在隐藏字段中在幕后跟踪——但是这让内部类可以引用它们关联的封闭类的字段.

A static nested class is just a nested class that doesn't require an instance of its enclosing class. If you are familiar with C++, all classes in C++ are "static" classes. In Java, nested classes are not static by default (this non-static variety is also called an "inner class"), which means they require an instance of their outer class, which they track behind the scenes in a hidden field -- but this lets inner classes refer to fields of their associated enclosing class.

public class Outer {

    public class Inner { }

    public static class StaticNested { }

    public void method () {
        // non-static methods can instantiate static and non-static nested classes
        Inner i = new Inner(); // 'this' is the implied enclosing instance
        StaticNested s = new StaticNested();
    }

    public static void staticMethod () {
        Inner i = new Inner(); // <-- ERROR! there's no enclosing instance, so cant do this
        StaticNested s = new StaticNested(); // ok: no enclosing instance needed

        // but we can create an Inner if we have an Outer: 
        Outer o = new Outer();
        Inner oi = o.new Inner(); // ok: 'o' is the enclosing instance
    }

}

How to在静态方法中实例化非静态内部类

我实际上默认将所有嵌套类声明为静态,除非我特别需要访问封闭类的字段.

I actually declare all nested classes static by default unless I specifically need access to the enclosing class's fields.

这篇关于可以在 Java 中实例化静态嵌套类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆