以下單級工作正常,使用「this」與不在Java的單例類中使用它?
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
。爲什麼是這樣?
沒有勺子。 – user1329572 2012-04-17 01:19:46
'this'只存在於對象實例的上下文中,而不存在於靜態上下文中。 – 2012-04-17 01:19:50
在靜態上下文中,您可以使用'Elvis.elvis'而不是'this.elvis'。 – 2012-04-17 01:21:46