-4

For example, I have a private member initialized to String str = "A". After the program finishes, I want str to become "B". Then, next day when I run the same program, str's value is "B", not "A".

Thank you.

1
  • the next day ?! Sounds like you are telling a story.
    – Raptor
    Mar 21, 2013 at 6:08

3 Answers 3

5

You need to write your data to a persistent storage location, such as a database or a file on disk.

7
  • Yes, I could write it to a file, but since it's a very small data. I am wondering if there is any way to avoid that.
    – user697911
    Mar 21, 2013 at 2:58
  • @user697911 Either the program never stops executing (a bad idea - what if your computer crashes?) or it writes the data to a file or database. Your choice ;)
    – Patashu
    Mar 21, 2013 at 2:59
  • @user697911: You may want to use properties file.
    – SLaks
    Mar 21, 2013 at 3:00
  • Why a programing language doesn't support this feature? It seems to be a very useful one, and a simple thing to do.
    – user697911
    Mar 21, 2013 at 3:01
  • Property file is still an physical file who values have to be read and write out?
    – user697911
    Mar 21, 2013 at 3:02
2

Would you please tell more on "next day when I run the same program, str's value is "B", not "A". Where does urs program resides?Are u starting program again by starting a server ? At last exit point of program call following code import java.lang.reflect.Field;

public class Reflection {
    String str = "A";
    void test(){
        Class test=    Reflection.class;
        try {
      Field aa=      test.getDeclaredField("str");
      aa.setAccessible(true);
      try {

        aa.set(this, "B");
        System.out.println(str);
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


public static void main(String[] args) {
    Reflection test=new Reflection();
    test.test();
}
}
1

(Expanding on SLaks answer a bit) - if you want to persist any information in a program between executions, that information needs to be stored somewhere external to the program. This can be a database (often for large/complex programs) or a file (for small programs), or anything in between (offsite cloud storage, etc). The same concept is used in any desktop application that 'saves documents' - it's basically saved state in a file that you can reload through the program.

A simple example in Java is to use a properties file - this question gives an example of how to write information to a properties file - you can use related API to read those properties back in when you start your application the next time.

If you're more adventurous, there are several frameworks in Java that provide persistence features, but this sounds like much more complexity than you're looking for in your app.

Not the answer you're looking for? Browse other questions tagged or ask your own question.