常量和内部类 [英] Constants and inner classes

查看:46
本文介绍了常量和内部类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    内部类中的
  1. static 变量:内部类不能包含 static 字段.它不能包含 static 成员,因为在哪里分配 static 成员会有问题.内在阶级与外在阶级联系在一起.我了解为什么它不包含 static 成员,但是内部类可以包含 static 常量.为什么?是否经过特殊处理?在特殊堆上吗?仍然是静态成员,但是是常量,因此是否经过特殊处理?

  1. static variables in inner classes: inner class cannot contain static fields. It cannot contain static members, because there would be problem where to assign static member. Inner class is connected with outer class. I understand why it does not contain static member, but inner class can contain static constant. Why? Is it specially treated? Is it on special heap? Still it is static member, but constant, so it is specially treated?

它可以包含:"final static int x",但不能包含"static int x".

It can contain: "final static int x", but not "static int x".

我想知道为什么本地类中使用的方法的变量应该是 final .我的意思是:

I wonder why variables of the method used in local class should be final. I mean:

public void function1() {
  final int x; // value of x, which is used in class A have to be final

  class A {
    void function2() {
      //body of function
    }
  }
}

答案是:将变量x id复制到类A.无法更改,因为这样会出现不一致.那么,为什么Java的架构师没有创建不复制变量x的语言呢?变量的地址将被传递,然后变量x将被更改而不会出现不一致的情况.有人可以给我举例说明一下吗?

The answer is: variable x id copied to class A. It cannot be changed, because then there appeared inconsistency. So why the architects of Java did not create language where variable x is not copied? The address of the variable would be passed and then variable x would be changed without inconsistency. Can somebody give me example and explain this?

或与同步有关的问题.这两个变量应该相同,但是如果我们超出范围呢?变量之一不存在,那么同步又如何呢?

Or issue connected with synchronization. Both variables should be the same, but what if we are out of the scope? One of the variables does not exits, so what with synchronization?

为什么有效:

interface In
{
    void f1();
}

class A
{
    int variable = 3;

    In g()
    {
        return new In()
        {
            @Override
            public void f1()
            {
                variable = 6;

                System.out.println(variable);
            }
        };
    }
}

public static void main(String[] args)
    {
        In in1;
        {
            A a = new A();
            in1 = a.g();
        }

        in1.f1(); //class A does not exists any more, field 'variable' is changed, despite it does not exist
    }

为什么没有同步问题?

推荐答案

要回答第一个问题:

就我个人而言,我还不明白为什么它不接受内部类中的非最终静态成员.但是,我可以告诉您编译时会发生什么.当您声明 static final primitive 成员时,可以将其编译为使用该成员的代码.但是,当您尝试创建 o静态最终对象o = new Object(); 时,它无法在编译时知道 o 所指向的对象.显然会产生与在内部类中创建 static int (非最终)相同的编译器错误.

Personally, I don't see yet why it doesn't accept non-final static members in inner classes. However, I can tell you what happens on compile time. When you declare a static final primitive member, it can be compiled into the code that uses this member. However, when you try to create a static final Object o = new Object();, it cannot know on compile time what the o will be pointing at. And obviously gives the same compiler error as if you create a static int (non-final) in your inner-class.

要回答第二个问题,这是为什么:

To answer the second question, here is why:

x 是一个局部变量,被压入堆栈.当您在内部类中使用对 x 的引用时,您将遇到问题.因为内部类最有可能比该函数的生存期更长.该函数结束后, x 会超出范围并从堆栈中删除.因此,现在,您的内部类仍然具有对不再存在的变量的引用.因此,这是在编译器中实现的一个小技巧:如果将 x 声明为 final ,则编译器知道它不会更改,因此没有必要保留引用它.这样,编译器就可以将 x 作为新成员复制到内部类的实例中.因此, x 将被复制到堆中.如果您不将其标记为final,则将其与对堆栈的无效引用进行比较(当然,它不会编译,因为编译器可以保护您避免出现此错误).

x is a local variable, which is pushed onto the stack. When you use a reference to x in your inner class, you will have problems. Because the inner class is most likely going to live longer than the scope of that function. As soon as the function ends, x goes out of scope and gets removed from the stack. So, right now, your inner class still has a reference to a variable that doesn't exist anymore. So, this is a little trick implemented in the compiler: if you declare your x as final the compiler knows it won't change, so there is no point of keeping a reference to it. This allows the compiler to copy x into the instance of inner class as a new member. Thus, x will be copied to the heap. Compare this to the invalid reference to the stack if you do not mark it as final (which doesn't compile of course, because the compiler protects you of making this mistake).

这篇关于常量和内部类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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