0

Suppose there is a variable

int a = 10;

when I run the program again and access the data it's the same when I access it somehow. And then I am able to change it, suppose to

a = a + 6;

And after that, unless until I change it to something else, the value remain the same.

Is there anyway to do something like that?

Note: Don't want to use files, and or external database.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Junaid Khan
  • 520
  • 6
  • 15
  • 2
    So, you want to have a persistent state without persisting it anywhere??? – Oleg Feb 03 '18 at 13:06
  • Not exactly, Obviously it's going to be saved somewhere, just want it to be saved in a place where it can't be seen, files are seen. Database needs to be connected and all. So, other way if there is any? – Junaid Khan Feb 03 '18 at 13:09
  • @JunaidKhan what's your definition of "where it can't be seen" in this context? Is it secret? How about string it in the environment variables? – syncdk Feb 03 '18 at 13:13
  • I am beginner in java, So my definition is, "it can't be seen in the file-system", I have no knowledge about environment variables? Answer with some code, if they can do what I want. – Junaid Khan Feb 03 '18 at 13:18

3 Answers3

2

Download Java Prefernces API: https://docs.oracle.com/javase/8/docs/technotes/guides/preferences/index.html

And then type:

    import java.util.prefs.*;

    public class Example {

        public static final int a = 10;

        public void savePreference(int value) {
           Preferences prefs = Preferences.userNodeForPackage(Example.class);                
           prefs.put(a, value); 
        }

        public int readPreference() {
           Preferences prefs = Preferences.userNodeForPackage(Example.class);
           return prefs.get(a, "default");  
        }  

        public void main (String args[]){

        // Read 
        myValue = readPreferences();

        // Modify 
        myValue = myValue + 6;

        // Save
        savePreferences(myValue);            

        }

    }

Hope this helps

Employee
  • 3,109
  • 5
  • 31
  • 50
1

No. The program state only exists during its execution.

You must store the value of a outside of your application somehow if you want its state to persist between executions.

If you don't want to use files or a database you could use the Java preferences API.

You could also use environment variables but it's tricky.

syncdk
  • 2,820
  • 3
  • 25
  • 31
0

A little more for total beginners like me. Thanks to Employee

import java.util.prefs.Preferences;

public class DataClass {

public void createPreference(String name, int value) {
    Preferences prefs = Preferences.userNodeForPackage(DataClass.class);
    if (prefs.getInt(name, -1) == -1) {
        prefs.putInt(name, value);
        System.out.println("Created For you, "+name+" with value: "+value);
    } else {
        System.out.println("Already Exist");
    }
}

public int readPreference(String name) {
    Preferences prefs = Preferences.userNodeForPackage(DataClass.class);
    return prefs.getInt(name, -1);
}

public void updatePreference(String name) {
    Preferences prefs = Preferences.userNodeForPackage(DataClass.class);
    int val = prefs.getInt(name, -1);
    if (val != -1) {
        val++;
        prefs.remove(name);
        prefs.putInt(name, val);
        System.out.println("New Value of "+name+" is: "+prefs.getInt(name, -1));
    }
}

public void removePreference(String key) {
    Preferences prefs = Preferences.userNodeForPackage(DataClass.class);
    if(prefs.getInt(key, -1) != -1){
        prefs.remove(key);
        System.out.println("Removed: "+key);
    }else{
        System.out.println("No Such Data in: "+key);
    }
}

}
Junaid Khan
  • 520
  • 6
  • 15