34

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 in one java file, then can I use the properties anywhere? But this is not true, I can't use it anywhere, only in the same java file I called setProperty.

I really do not know why and what the function of this method is.

Akshay Arora
  • 729
  • 1
  • 8
  • 20
Rocky Hu
  • 1,326
  • 4
  • 17
  • 32

4 Answers4

77

System class has a static member variable named props which is of type Properties. Adding to that, Properties is a subtype of Hashtable class. All the property values are stored as Key and Value. So, datastore is Hashtable.Answering the other question, You can very well use System.getProperty(propertyKey) method throughout your application since it is a public static method. You haven't understood how java programs work. When you run a Java program, you are actually starting a JVM instance. That instance will have its own System properties. That is where you have to put your property. When you run the other program, that will have its own System properties. So, you cannot expect a property which you set in one JVM instance to be accessible from another JVM instance! You can access the System.getProperty(propertyKey) in all classes running in the same JVM instance. Hope you can understand!

Keerthivasan
  • 12,760
  • 2
  • 32
  • 53
  • Would be nice if there would be a property "shared" or "persisted" to get it after a restart again. – Pwnstar Sep 27 '18 at 11:31
  • @Spektakulatius we can have an external shared resource for storing such properties that can be used for later retrieval. This shared resource can be DB, config file or anything else that meets the same goal. – Mandar Autade Nov 13 '19 at 07:14
3

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<Object,Object>

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).

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
1

In the first Scenario, the moment A.java run, the JVM will stop and it will release all the values. And when the B.java run, the JVM will start fresh. So, the value will not persist across.

In the second scenario, the JVM will not stop between the executions. So, the property value will persist from the second program.

Arun
  • 11
  • 1
0

The data is stored in memory as long as your JVM instance is up. It it really isn't related to the file which called the methods.

How are you running your application? Is it a web application stored in an application sever? Can you post a sample of your code?

Pedro Rio
  • 1,444
  • 11
  • 16