下面的单例类工作正常,
public class Elvis
{
private static Elvis elvis = new Elvis();
private Elvis()
{
}
public static Elvis Instance()
{
return elvis;
}
}
但是,当我将 return elvis;
更改为 return this.elvis
时,我得到了 non-static variable this cannot be referenced from a static context
。这是为什么?
最佳答案
this
引用当前对象实例。 static
方法不包含在对象中,它包含在类中。
关于java - 在 Java 的单例类中使用 "this"还是不使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10183830/