"this"是什么意思初始化程序块中的关键字是什么意思? [英] what does "this" keyword mean in the initializer block?

查看:36
本文介绍了"this"是什么意思初始化程序块中的关键字是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

class StaticBlock {
    {       
        println("initializer block : " + message);
    }
    public StaticBlock(String message) {
        this.message = message;
    }
    private String message;
}

现在的问题是,在初始化块中

Now the issue is that, in the initializer block

{       
    println("initializer block : " + message);
}

如果我在 message 之前添加 this 关键字,它可以工作,但是缺少 this 关键字时会出错.

if I add the this keyword before message, it works, but there is an error when missing this keyword.

编译器说:

StaticBlockDemo.java:34: illegal forward reference
                println("initializer block : " + message);
                                                 ^
1 error

为什么它们不一样?

推荐答案

我不知道设计原理,但阅读 Java 语言规范的相关部分可能会有所帮助.

I don't know the design rationale, but it may help to read the relevant sections of the Java language specification.

8.6.实例初始化器

允许实例初始化器通过关键字this (§15.8.3) 来引用当前对象,以使用关键字super (§15.11.2, §15.12),并在范围内使用任何类型变量.

Instance initializers are permitted to refer to the current object via the keyword this (§15.8.3), to use the keyword super (§15.11.2, §15.12), and to use any type variables in scope.

使用在使用后以文本形式出现的声明的实例变量的使用有时会受到限制,即使这些实例变量在范围内.请参阅第 8.3.2.3 节,了解管理对实例变量的前向引用的精确规则.

Use of instance variables whose declarations appear textually after the use is sometimes restricted, even though these instance variables are in scope. See §8.3.2.3 for the precise rules governing forward reference to instance variables.

8.3.2.3.初始化期间对字段使用的限制

仅当成员是类或接口的实例(分别为static)字段C和所有满足以下条件之一:

The declaration of a member needs to appear textually before it is used only if the member is an instance (respectively static) field of a class or interface C and all of the following conditions hold:

  • 用法发生在C的实例(分别为static)变量初始化器或C的实例(分别为静态)初始化器中代码>.

  • The usage occurs in an instance (respectively static) variable initializer of C or in an instance (respectively static) initializer of C.

用法不在作业的左侧.

用法是通过一个简单的名称.

The usage is via a simple name.

C 是包含用法的最里面的类或接口.

C is the innermost class or interface enclosing the usage.

以下是规范的该部分中的示例,该示例经过精简以说明您所询问的具体问题:

Here's the example from that section of the spec trimmed down to illustrate the specific point you're asking about:

class UseBeforeDeclaration {

    {
        j = 200;
          // ok - assignment
        j = j + 1;
          // error - right hand side reads before declaration
        int k = j = j + 1;
          // error - illegal forward reference to j
        int n = j = 300;
          // ok - j at left hand side of assignment
        int h = j++;
          // error - read before declaration
        int l = this.j * 3;
          // ok - not accessed via simple name
    }

    int j;
}

不过,我还应该注意,即使您的代码的编译版本也不会执行您想要的操作.如果你运行它:

I should also note, though, that even the compiling version of your code isn't going to do what you want. If you run it:

new StaticBlock("abc");

它会打印

initializer block : null

这是因为初始化器在(大部分)构造函数体之前执行.以下是规范中的要点:

This is because initializers are executed before (most of) the constructor body. Here are the salient points from the spec:

12.5.创建新的类实例

[...] 使用以下过程处理指定的构造函数以初始化新对象:

[...] the indicated constructor is processed to initialize the new object using the following procedure:

[...]

4. 为这个类执行实例初始化器和实例变量初始化器[...]

 4. Execute the instance initializers and instance variable initializers for this class [...]

5. 执行此构造函数的其余部分.[...]

 5. Execute the rest of the body of this constructor. [...]

这篇关于"this"是什么意思初始化程序块中的关键字是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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