在内存中保存值并获取它

Save value in memory and get it

我正在开发 swing 应用程序,下面的步骤解释了我的问题。

  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.

现在的问题是拼贴 class 的对象已被销毁,因为弹出窗口已关闭,所以我如何才能将选定的拼贴值存储在 Java 的内存中.

代码示例:

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();
    }
}

我想要使用第二个对象的值 1,它应该像 "second.getData()" 应该带值 1。是否可能,如果是,那么如何? 谢谢

你也可以试试,

我一直在代表想法,现在你可以根据自己的需求领先,

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


      }

}

已编辑

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

两种方法都可以,

确定如果你选择这种方式,

Sample second = first;

然后在 firstsecond 上进行更改的任何地方。 这应该反映在两个实例上,即 firstsecond。 因为它不是两个不同的实例。 这只是一个例子。

但是如果你选择这种方式,

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

那么它与 first 不同,这意味着如果您在 second 上进行了任何更改,那么它不应该 反映 first 上。

所以,你可以随便,你很方便。 祝你好运。