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

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

问题描述

来自Oracle的Java 教程我发现了这个文本:


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



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



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



OuterClass.StaticNestedClass



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



OuterClass.StaticNestedClass nestedObject =
new OuterClass.StaticNestedClass();


我认为不可能实例化一个静态类,所以我真的不懂粗体句。



你知道这意味着什么吗?

解决方案

你要么像kihero所说的那样将 static abstract 混淆,要么你正在用一个<$ c的类混淆这个概念$ c> static 方法(这只是碰巧有静态方法的类)。



静态嵌套类只是一个嵌套类这不需要其封闭类的实例。如果您熟悉C ++,那么 C ++中的所有类都是静态类。在Java中,嵌套类默认情况下不是静态的(这种非静态类型也称为内部类),这意味着它们需要一个外部类的实例,它们在隐藏字段的后台跟踪 - 但是这允许内部类引用其相关封闭类的字段。

  public class Outer {

public class Inner {}

public static class StaticNested {}

public void method(){
//非静态方法可以实例化静态和非静态嵌套class
Inner i = new Inner(); //'this'是隐含的封闭实例
StaticNested s = new StaticNested();
}

public static void staticMethod(){
Inner i = new Inner(); //< - 错误!没有封闭的实例,所以不能这样做
StaticNested s = new StaticNested(); // ok:没有封闭的实例需要

//但如果我们有一个Outer我们可以创建一个Inner:
Outer o = new Outer();
Inner oi = o.new Inner(); // ok:'o'是封闭的实例
}

}

如何进行大量其他示例在静态方法中实例化非静态内部类



我实际上默认声明所有嵌套类是静态的,除非我特别需要访问封闭类的字段。 / p>

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

For example, to create an object for the static nested class, use this syntax:

OuterClass.StaticNestedClass nestedObject = 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?

解决方案

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).

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
    }

}

Lots of other examples at How to instantiate non static inner class within a static method

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

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

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