English 中文(简体)
“这种替罪羊”的警告是什么,我如何处理?
原标题:What is a "this-escape" warning, and how do I deal with it?

我努力寻找这方面的资源,然而,我的许多班子在汇编我关于最近贾瓦(21)的法典时正陷入这一错误。

这里是一个法典的例子。

public class ThisEscapeExample
{

        public Object o;

        public ThisEscapeExample()
        {

                this.overridableMethod();

        }

        public void overridableMethod()
        {

                this.o = new Object();

        }

}

这里是我的汇编指挥。

javac -Xlint:all ThisEscapeExample.java
ThisEscapeExample.java:9: warning: [this-escape] possible  this  escape before subclass is fully initialized
                this.overridableMethod();
                                      ^
1 warning
最佳回答

这里是JDK Bug系统条目,介绍这一新的警告——:https://bugs. openjdk.org/browse/JDK-8299995

短篇故事,this-einski预警,警告你,如果子级能够@ Override a means that is also calls in the Superclass Constructionor.

这样做是危险的,因为压倒了建筑商使用的一种方法,使得子级在子类初始化过程中无意地引入ug。 如果这一方法取决于尚未建立起来的国家,因为我们仍在超级建筑中? 毕竟,在要求超级建筑商之前,你不能在子楼做任何事情(

有一些办法可以纠正这种情况。

  1. 只有在建筑商中使用不能压倒一切的方法。

    • <编码>static methods。

    • http://www.ohchr.org。 有待于SAME CLASS、NO PARENT FINAL INSTTHODS

    • <代码>私人方法。

  2. 加入:

  3. http://www.un.org/Depts/DGACM/index_chinese.htm

请注意,这些规则可追溯适用。 当你在建筑商中使用一种方法时,这种方法不仅必须“不可再利用”,而且该方法通过<条码>的<条/条码>进入必须<><><>>>>>>符合上述一条规则。 如果您的顶级方法不能压倒一切,但其内部的方法之一是,而且该方法在范围上有<条码>,则在汇编时,你将收到<条码>这一代名列/代码错误。 这里就是一个例子。

import javax.swing.*;

public class GUI
{

   private final JFrame frame;

   public GUI()
   {
   
      this.frame = new JFrame();
   
      this.frame.add(this.createBottomPanel());
   
   }

   //final! Does that mean we are safe?
   final JPanel createBottomPanel()
   {
   
      final JButton save = new JButton();
   
      save
         .addActionListener
         (
            /*
             * No. We get the warning here at the start of this lambda.
             * The reason is because we have given this lambda the
             * ability to call this.toString(), and we don t know when
             * it will do that. Maybe now, maybe later. But if it does
             * it now, then we could end up doing things before the
             * object is fully created. And if that were to happen, then
             * that would be a this-escape. So, the warning occurs here,
             * to let you know that it is possible.
             */
            actionEvent ->
            {
           
               this.toString();
           
            }
           
         )
         ;
   
      return null;
   
   }

}
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签