JavaSE7基礎 static 成員變量,被這個類的所有對象共享
阿新 • • 發佈:2017-07-08
out 對象 args 優秀 馬雲 pre light png try
版本參數:jdk-7u72-windows-i586
註意事項:博文內容僅供參考,不可用於其他用途。
代碼
class Test{ static String country = "China"; //static 成員變量,被這個類的所有對象共享 } class Demo{ public static void main(String[] args){ Test t1 = new Test(); Test t2 = new Test(); //t1/t2的country都是China System.out.println(t1.country); System.out.println(t2.country); //其中的一個對象改變其country,其他對象再用時,也被改變 t1.country = "USA"; System.out.println(t1.country); System.out.println(t2.country); } }
編譯與運行
——————————————————————————————————————————
Java優秀,值得努力學習。
學習資源:黑馬程序員_Java基礎視頻-深入淺出精華版/*來自黑馬雲課堂,網上可以找到*/
如果您覺得博文內容有可以優化的地方,請留下評論,我會認真思考的。
JavaSE7基礎 static 成員變量,被這個類的所有對象共享