如何“这个”在Java中转义构造函数? [英] How does "this" escape the constructor in Java?
本文介绍了如何“这个”在Java中转义构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我听说过这种情况发生在非线程安全的代码,因为不正确的构造的对象,但我真的没有这个概念下来,即使在阅读Goetz的书中。我想巩固我对这种代码气味的理解,因为我可能做到这一点,甚至没有意识到。请在您的解释中提供代码,使其坚持,谢谢。
I've heard about this happening in non thread-safe code due to improperly constructed objects but I really don't have the concept down, even after reading about in in Goetz's book. I'd like to solidify my understanding of this code smell as I maybe doing it and not even realize it. Please provide code in your explanation to make it stick, thanks.
推荐答案
非常简单的示例:
public class Test
{
private static Test lastCreatedInstance;
public Test()
{
lastCreatedInstance = this;
}
}
这篇关于如何“这个”在Java中转义构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文