Java - 在构造函数中泄露它 [英] Java - Leaking this in constructor

查看:551
本文介绍了Java - 在构造函数中泄露它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想避免(大部分)Netbeans 6.9.1的警告,我有一个问题与'泄漏在构造函数'警告。 / p>

我理解问题,在构造函数中调用一个方法并传递 this 是危险的,因为可能没有完全初始化。



这很容易修复我的单例类中的警告,



旧代码(简化):

 

code> private Singleton(){
...
addWindowFocusListener(this);
}

public static Singleton getInstance(){

...
instance = new Singleton();
...
}

新代码(简化):

  private Singleton(){
...
}

public static Singleton getInstance (){

...
instance = new Singleton();
addWindowFocusListener(instance);
...
}

如果构造函数是公共和可以从其他类调用。如何修复以下代码:

  public class MyClass {

...
List< MyClass> instances = new ArrayList< MyClass>();
...

public MyClass(){
...
instances.add(this);
}

}

不需要使用这个类来修改所有的代码(通过调用init方法)。

解决方案

instances.add(this)放在构造函数的结尾你应该IMHO是安全的告诉编译器只是抑制警告 strong>(*)。



如果你知道你在做什么,你可以使用一个警告,这个警告的性质不一定意味着有错误, @SuppressWarnings 注释。和注释中提到的Terrel一样,下面的注释就像NetBeans 6.9.1一样:

  @SuppressWarnings(LeakingThisInConstructor) 

(*)更新: Isthar和Sergey指出,其中泄漏构造函数代码可以看起来完全安全(如在你的问题),但它不是。有更多的读者可以批准这个?我正在考虑删除这个答案,因为上述原因。


I'd like to avoid (most of the) warnings of Netbeans 6.9.1, and I have a problem with the 'Leaking this in constructor' warning.

I understand the problem, calling a method in the constructor and passing "this" is dangerous, since "this" may not have been fully initialized.

It was easy to fix the warning in my singleton classes, because the constructor is private and only called from the same class.

Old code (simplified):

private Singleton() {
  ...
  addWindowFocusListener(this);
}

public static Singleton getInstance() {

  ...
  instance = new Singleton();
  ...
}

New code (simplified):

private Singleton() {
  ...
}

public static Singleton getInstance() {

  ...
  instance = new Singleton();
  addWindowFocusListener( instance );
  ...
}

This fix is not working if the constructor is public and can be called from other classes. How is it possible to fix the following code:

public class MyClass {

  ...
  List<MyClass> instances = new ArrayList<MyClass>();
  ...

  public MyClass() {
    ...
    instances.add(this);
  }

}

Of course I want a fix which does not require to modify all my codes using this class ( by calling an init method for instance).

解决方案

Since you make sure to put your instances.add(this) at the end of the constructor you should IMHO be safe to tell the compiler to simply suppress the warning (*). A warning, by its nature, doesn't necessarily mean that there's something wrong, it just requires your attention.

If you know what you're doing you can use a @SuppressWarnings annotation. Like Terrel mentioned in his comments, the following annotation does it as of NetBeans 6.9.1:

@SuppressWarnings("LeakingThisInConstructor")

(*) Update: As Isthar and Sergey pointed out there are cases where "leaking" constructor code can look perfectly safe (as in your question) and yet it is not. Are there more readers that can approve this? I am considering deleting this answer for the mentioned reasons.

这篇关于Java - 在构造函数中泄露它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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