Search code examples
javaswing

Save value in memory and get it


I am working on swing application, the steps below explains my problem.

  1. I have a dialog box where I need to select the College.

  2. Once I select the department, that window will be closed with the "dispose();" method.

  3. Then one more window will pop-up, where I have to select the department based on the selected college.

Now the issue is the object of the Collage class has been destroyed, as that pop-up has been closed, so how can I can store the selected collage value in memory in Java.

Code Example :

public class Test
{
    public static void main(String args[])
    {
        Sample first = new Sample();
        first.setData(1);

        Sample second = new Sample();
        System.out.println(first.getData());  // 1
        System.out.println(second.getData()); // 0
    }

}


public class Sample
{
    private int number;

    public int getNumber()
    {
        return number;
    }

    public void setNumber(int number)
    {
        this.number = number;
    }

    public void setData(int a)
    {
        setNumber(a);
    }

    public int getData()
    {
        return getNumber();
    }
}

I want value 1 using the second object, it should be like "second.getData()" should bring the value 1. is it possible if yes then how ? Thanks.


Solution

  • You can try somewhat likewise,

    I have been represent idea , now you can lead ahead by your own requirement-wise,

    public class InputSelect {
    
        public static final String[] colleges = { "College-1", "College-2", "College-3", "College-4" };
    
        public static final String[][] dept = {{"college-1 Department-1","college-1 Department-2"},{"college-2 Department-1","college-2 Department-2"},{"college-3 Department-1","college-3 Department-2"},{"college-4 Department-1","college-4 Department-2"}};
    
          public static void main(String[] args)
          {
    
            JFrame frame = new JFrame("Input Dialog Example 3");
            String favoritecollege = (String) JOptionPane.showInputDialog(frame, 
                "Select Your Choice Colleges : ",
                "Choose College",
                JOptionPane.QUESTION_MESSAGE, 
                null, 
                colleges, 
                colleges[0]);
    
            int index = 0;
    
            for(String clg : colleges){
                if(favoritecollege.equals(clg)){
                    break;
                }else{
                    index++;
                }
            }
    
            String favouriteDept = (String) JOptionPane.showInputDialog(frame, 
                    "Select Your Choice Department : ",
                    "Choose Dept of "+ favoritecollege +" college : ",
                    JOptionPane.QUESTION_MESSAGE, 
                    null, 
                    dept[index], 
                    dept[index][0]);
    
            System.out.printf("Favorite College is %s.\n", favoritecollege);
            System.out.printf("Favorite Dept is %s.\n", favouriteDept);
    
    
          }
    
    }
    

    EDITED

    public static void main(String args[])
        {
            Sample first = new Sample();
            first.setData(1);
            //Either
            //Sample second = new Sample();
            //second.setData(1);
            //or 
            Sample second = first;
    
            System.out.println(first.getData());  // 1
            System.out.println(second.getData()); // here, you will get 1 after above changes made.
        }
    

    You can do Either way,

    Make sure if you choose this way,

    Sample second = first;
    

    then wherever you made changes Either on first or second. that should be reflect on both instance i.e. first and second. because it's not 2-different instance. it's just one instance.

    but if you choose this way,

    Sample second = new Sample();
    second.setData(1);
    

    then it's different then first, means if you made any change on second then it should not reflect on first.

    So, you can do whatever way, you are convenient . Best of luck.