/*
* 設置指定鍵對值的系統屬性
* setProperty (String prop, String value);
*
* 參數:
* prop - 系統屬性的名稱。
* value - 系統屬性的值。
*
* 返回:
* 系統屬性以前的值,如果沒有以前的值,則返回 null。
*
* 拋出:
* SecurityException - 如果安全管理器存在並且其 checkPermission 方法不允許設置指定屬性。
* NullPointerException - 如果 key 或 value 為 null。
* IllegalArgumentException - 如果 key 為空。
* 注:這里的system,系統指的是 JRE (runtime)system,不是指 OS。
*
*/
//實例
System.setProperty("Property1", "abc");
System.setProperty("Property2","def");
//這樣就把第一個參數設置成為系統的全局變量!可以在項目的任何一個地方 通過System.getProperty("變量");來獲得,
//System.setProperty 相當於一個靜態變量 ,存在內存里面!
public class SystemTest { static { setValue(); } public static void setValue() { System.setProperty("name", "張三"); System.setProperty("age", "28"); } public static void main(String[] args) { System.out.println(System.getProperty("name")); System.out.println(System.getProperty("age")); } }
輸出: