System.setProperty and System.getProperty

后端 未结 4 1722
太阳男子
太阳男子 2020-12-23 21:09

I didn\'t understand when I used System.setProperty to define a parameter, where the data is stored?

If say that I used System.setProperty

4条回答
  •  长情又很酷
    2020-12-23 21:26

    If you see source code of System Class it has following class variable

    private static Properties props;
    

    As for properties class you can think of it as a HashMap. It actually extends HashMap.

    public class Properties extends Hashtable
    

    When you call

    setProperty(String key, String value)
    

    it actually does

    props.setProperty(key, value);
    

    This is just the summary(Security manager checks are also involved).

    Now why did I say it is per JVM instance?

    When you start a Java process a separate JVM instance is created that runs your process. Also since props is a Class variable(not an instance variable) just one copy of it will be present in the corresponding Class instance which will be set when that class is loaded. Now this is under the assumption that you do not have any of your custom class loaders in which case behavior might be different. But for simplistic scenario you System.setProperty() and System.getProperty() will set system properties that you can access via any class running as a part of that java process(JVM).

提交回复
热议问题