什么是“T是顶级类,并且执行词汇嵌套在T中的断言语句”。意思? [英] What does "T is a top-level class, and an assert statement lexically nested within T is executed." mean?

查看:216
本文介绍了什么是“T是顶级类,并且执行词汇嵌套在T中的断言语句”。意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习类和接口的初始化,并且它说T是顶级类,并且在T中执行词法嵌套的断言语句。
任何人都可以告诉我T是一个顶级类,并且在T中执行词法嵌套的断言语句。通过一个例子来表示?

I am learning the " Initialization of Classes and Interfaces", and it says "T is a top-level class, and an assert statement lexically nested within T is executed." Could any one tell me what does "T is a top-level class, and an assert statement lexically nested within T is executed." mean by a example?

这句话来自 JLS ,原始文本如下:

This sentence is from JLS, and original text is like this :


类或接口类型T将在第一次出现以下任何一个之前立即初始化:

A class or interface type T will be initialized immediately before the first occurrence of any one of the following:


  • T是一个类,创建了一个T实例。

  • T是一个类,调用T声明的静态方法。

  • T声明的静态字段是已分配。

  • 使用T声明的静态字段,该字段不是常量变量(§4.12.4)。

  • T是a顶级类,以及在词典内嵌套在T中的断言语句(第14.10节)被执行。

  • T is a class and an instance of T is created.
  • T is a class and a static method declared by T is invoked.
  • A static field declared by T is assigned.
  • A static field declared by T is used and the field is not a constant variable (§4.12.4).
  • T is a top-level class, and an assert statement (§14.10) lexically nested within T is executed.


推荐答案

我可以给它一个部分解释。它指的是启用/禁用断言。断言由 -ea vm参数启用。

I can give a partial explanation to it. It refers to enabling/disabling assertion. Assertion is enabled by -ea vm argument.

关于断言的重要观点是:


启用在类完成初始化之前执行的断言语句。

An assert statement that is executed before its class has completed initialization is enabled.

假设没有给出 -ea 并运行以下代码:

Suppose -ea is not given and you run the below code:

 public class Q1 {
    public static void main(String[] args) {
        Bar b = new Bar();
    }
}
class Bar {
    static {
        boolean enabled = false;
        assert  enabled = false; //line(a)
        System.out.println("Asserts " + 
               (enabled ? "enabled" : "disabled"));
        System.out.println("as");
        Baz.testAsserts();
    }
}
class Baz extends Bar {
    static void testAsserts() {
        boolean enabled = false;
        assert  enabled = false;
        System.out.println("Asserts " + 
               (enabled ? "enabled" : "disabled"));
    }
}

在上面的示例中 b 被初始化,Java保证在调用 line(a)之前,断言被禁用(即,行(a)根本不被执行)。因为断言启用/禁用是类初始化的一部分,因此在您显示的语句中提到它。

In the above example when b is initialized, Java guarantees that before line(a) is called, the assertion is disabled (i.e. line(a) is not executed at all). Because assert enable/disable is a part of class initalization, hence it is mentioned in your shown statement in question.

原因,为什么顶级类是提到而不是所有其他类都是这个。这里有更详细的行为:

The reason, why top-level class is mentioned and not every other class is this. More detailed behavior here:

public class Q1 {
    public static void main(String[] args) {
        Baz.testAsserts(); 
        // Will execute after Baz is initialized.
    }
}
class Bar {
    static {
        Baz.testAsserts();
        // Will execute before Baz is initialized!
    }
}
class Baz extends Bar {
    static void testAsserts() {
        boolean enabled = false;
        assert  enabled = false;
        System.out.println("Asserts " + 
               (enabled ? "enabled" : "disabled"));
    }
}

甚至 -ea 标志未使用,仍会抛出 AssertionException 。以下是发生的情况:

Even -ea flag is not used, still it throws an AssertionException. Here is what happens:


  1. Q1.main被称为

  2. Q1.main调用Baz.testAsserts 。

  3. 因为Baz扩展Bar和Bar没有初始化,所以根据JLS它会尝试初始化Bar

  4. 调用Bar的静态块。记住assert语句在其类完成初始化或调用assert(首先发生)之前已启用。在这种情况下, true 在此阶段 Bar 仍未完全初始化

  5. 静态 Bar 调用 Baz.testAsserts()。断言仍然被启用(记得禁用断言与类初始化有关,而Bar仍未完全初始化)。现在Baz.testAsserts()抛出 AssertionException

  1. Q1.main is called
  2. Q1.main calls Baz.testAsserts.
  3. Because Baz extends Bar and Bar is not initialized, as per JLS it tries to initialize Bar
  4. static block of Bar is called. Remember assert statement is enabled before its class has completed initialization or assert is called (which ever happens first). Which in this case is true at this stage as Bar is still not completely initialized
  5. static of Bar calls Baz.testAsserts(). The assert is still enabled (remember disabling assertion has got to do with class initialization and Bar is still not completely initialized). Now Baz.testAsserts() throws AssertionException.

上面是一个循环孔。 JLS只保证在顶级类中执行任何 assert 之前,它将禁用/启用(如同给出的任何vm参数)。但如果它不是顶级类,那么行为取决于顶级类的初始化。要解释这一点,请看:

Above is a loop hole. JLS only guarantees that before executing any assert in Top level class, it will disable/enable (as whatever vm argument is given) it. But if it is not a top level class, then the behavior depends on the initialization of top-level class. To explain this, see this:

class Bar {
    static {
        //Baz.testAsserts();
        boolean enabled = false;
        assert  enabled = false;
        System.out.println("Asserts " + 
               (enabled ? "enabled" : "disabled"));
        // Will execute before Baz is initialized!
    }
}
class Baz extends Bar {
    static void testAsserts() {
        boolean enabled = false;
        assert  enabled = false;
        System.out.println("Asserts " + 
               (enabled ? "enabled" : "disabled"));
    }
}

这打印 Asserts禁用断言已禁用 as Bar 已初始化。 Bar 初始化为该类禁用断言,因此 Baz

This prints Asserts disabled Asserts disabled as Bar is well initialized. Bar initialization disables assert for the class and hence for Baz.

这篇关于什么是“T是顶级类,并且执行词汇嵌套在T中的断言语句”。意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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