這是比較差異來解釋所有初學者的靜態關鍵詞。
當您更多地使用類和對象時,您將清楚地瞭解它。
| * | 靜態:靜態項目可以與類名被稱爲
如果你在代碼觀察,一些功能被直接調用類的名字,如
NamCls.NamFnc();
System.out.println();
這是因爲NamFnc和println西港島線使用前關鍵字靜態聲明他們。
| * | 非靜態:非靜態的物品可以用類變量
如果它不是靜態調用,您需要的類的變量,類變量後
投放點和
然後調用函數。
NamCls NamObjVar = new NamCls();
NamObjVar.NamFnc();
下面的代碼說明你整齊
| * |在類的靜態和非靜態函數:
public class NamCls
{
public static void main(String[] args)
{
PlsPrnFnc("Tst Txt");
NamCls NamObjVar = new NamCls();
NamObjVar.PrnFnc("Tst Txt");
}
static void PlsPrnFnc(String SrgPsgVal)
{
System.out.println(SrgPsgVal);
}
void PrnFnc(String SrgPsgVal)
{
System.out.println(SrgPsgVal);
}
}
| * |靜態和非靜態類的類中:
public class NamCls
{
public static void main(String[] args)
{
NamTicCls NamTicVaj = new NamTicCls();
NamTicVaj.PrnFnc("Tst Txt");
NamCls NamObjVar = new NamCls();
NamNicCls NamNicVar = NamObjVar.new NamNicCls();
NamNicVar.PrnFnc("Tst Txt");
}
static class NamTicCls
{
void PrnFnc(String SrgPsgVal)
{
System.out.println(SrgPsgVal);
}
}
class NamNicCls
{
void PrnFnc(String SrgPsgVal)
{
System.out.println(SrgPsgVal);
}
}
}
這可能幫助 - http://www.buggybread.com/2014/06/error-cannot-use-this-in-static-context.html – 2014-08-25 21:04:04
嘗試儘可能避免使用靜態。你可以編寫一個完整的程序,全部是靜態的,就像'C'一樣。但它不會是一個很好的。嘗試按照它所使用的方式使用Java,作爲面向對象的語言。 – 2015-09-15 16:21:57