Java:静态vs内部类[重复]

本文翻译自:Java: Static vs inner class [duplicate]

This question already has an answer here: 这个问题在这里已有答案:

What is the difference between static and non-static nested class? 静态和非静态嵌套类有什么区别?


#1楼

参考:https://stackoom.com/question/5g3Z/Java-静态vs内部类-重复


#2楼

An inner class, by definition , cannot be static, so I am going to recast your question as "What is the difference between static and non-static nested classes?" 根据定义 ,内部类不能是静态的,所以我要将你的问题改写为“静态和非静态嵌套类之间有什么区别?”

A non-static nested class has full access to the members of the class within which it is nested. 非静态嵌套类具有对其嵌套的类的成员的完全访问权。 A static nested class does not have a reference to a nesting instance, so a static nested class cannot invoke non-static methods or access non-static fields of an instance of the class within which it is nested. 静态嵌套类没有对嵌套实例的引用,因此静态嵌套类不能调用非静态方法或访问嵌套它的类实例的非静态字段。


#3楼

Let's look in the source of wisdom for such questions: Joshua Bloch's Effective Java : 让我们看看这些问题的智慧来源: Joshua Bloch的Effective Java

Technically, there is no such thing as a static inner class. 从技术上讲,没有静态的内部类。 According to Effective Java , the correct terminology is a static nested class . 根据Effective Java ,正确的术语是静态嵌套类 A non-static nested class is indeed an inner class, along with anonymous classes and local classes. 非静态嵌套类确实是一个内部类,以及匿名类和本地类。

And now to quote: 现在引用:

Each instance of a non-static nested class is implicitly associated with an enclosing instance of its containing class... It is possible to invoke methods on the enclosing instance. 非静态嵌套类的每个实例都隐式地与其包含类的封闭实例相关联......可以在封闭实例上调用方法。

A static nested class does not have access to the enclosing instance. 静态嵌套类无权访问封闭实例。 It uses less space too. 它也占用更少的空间。


#4楼

Discussing nested classes... 讨论嵌套类......

The difference is that a nested class declaration that is also static can be instantiated outside of the enclosing class. 不同之处在于,静态的嵌套类声明可以在封闭类之外实例化。

When you have a nested class declaration that is not static , Java won't let you instantiate it except via the enclosing class. 如果您有一个非静态的嵌套类声明,Java将不允许您实例化它,除非通过封闭类。 The object created out of the inner class is linked to the object created from the outer class, so the inner class can reference the fields of the outer. 从内部类创建的对象链接到从外部类创建的对象,因此内部类可以引用外部的字段。

But if it's static , then the link does not exist, the outer fields cannot be accessed (except via an ordinary reference like any other object) and you can therefore instantiate the nested class by itself. 但是如果它是静态的 ,那么链接就不存在了,外部字段不能被访问(除了通过像任何其他对象那样的普通引用),因此你可以自己实例化嵌套类。


#5楼

There are two differences between static inner and non static inner classes. 静态内部类和非静态内部类之间存在两个差异。

  1. In case of declaring member fields and methods, non static inner class cannot have static fields and methods. 在声明成员字段和方法的情况下,非静态内部类不能具有静态字段和方法。 But, in case of static inner class, can have static and non static fields and method. 但是,在静态内部类的情况下,可以有静态和非静态字段和方法。

  2. The instance of non static inner class is created with the reference of object of outer class, in which it has defined, this means it has enclosing instance. 非静态内部类的实例是使用外部类的对象的引用创建的,在其中定义了它,这意味着它具有封闭实例。 But the instance of static inner class is created without the reference of Outer class, which means it does not have enclosing instance. 但是静态内部类的实例是在没有外部类引用的情况下创建的,这意味着它没有封闭实例。

See this example 看这个例子

class A
{
    class B
    {
        // static int x; not allowed here
    }

    static class C
    {
        static int x; // allowed here
    }
}

class Test
{
    public static void main(String… str)
    {
        A a = new A();

        // Non-Static Inner Class
        // Requires enclosing instance
        A.B obj1 = a.new B(); 

        // Static Inner Class
        // No need for reference of object to the outer class
        A.C obj2 = new A.C(); 
    }
}

#6楼

An inner class cannot be static, so I am going to recast your question as "What is the difference between static and non-static nested classes?". 内部类不能是静态的,所以我要重新提出你的问题为“静态和非静态嵌套类之间有什么区别?”。

as u said here inner class cannot be static... i found the below code which is being given static....reason? 正如你所说的内部类不能是静态的......我发现下面的代码是静态的....原因? or which is correct.... 或哪个是正确的....

Yes, there is nothing in the semantics of a static nested type that would stop you from doing that. 是的,静态嵌套类型的语义中没有任何内容可以阻止您这样做。 This snippet runs fine. 这个片段运行正常。

    public class MultipleInner {
        static class Inner {
        }   
    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Inner();
        }
    }
}

this is a code posted in this website... 这是本网站发布的代码......

for the question---> Can a Static Nested Class be Instantiated Multiple Times? 对于问题--->静态嵌套类可以多次实例化吗?

answer was---> 答案是--->

Now, of course the nested type can do its own instance control (eg private constructors, singleton pattern, etc) but that has nothing to do with the fact that it's a nested type. 现在,嵌套类型当然可以执行自己的实例控件(例如私有构造函数,单例模式等),但这与嵌套类型无关。 Also, if the nested type is a static enum, of course you can't instantiate it at all. 此外,如果嵌套类型是静态枚举,当然您根本无法实例化它。

But in general, yes, a static nested type can be instantiated multiple times. 但总的来说,是的,静态嵌套类型可以多次实例化。

Note that technically, a static nested type is not an "inner" type. 请注意,从技术上讲,静态嵌套类型不是“内部”类型。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值