Java – how to make two classes share the same variable definition
What I really need is to be able to declare regular variables in the interface and implement the interface in two classes. I don't have to redeclare these interfaces in each class (i.e. class.data.variables instead of class.variables) Are there any ways to achieve different goals?
Provide more details Basically, I created a small drawing program that put jlabel on JPanel on JScrollPane Because I have a specific design for these jlabels (that is, they are not just used to draw airline objects that represent this application), I have a class that extends jlabel and adds my application specific variables to it Finally, I use these variables to read and write XML files to load and save their designs Because I can't use this extension class for my XML definition, because it screams at the parent class. Even if I tell it to use none as a visitor (I read a bug), I must create the same class and copy the values for saving and loading There are not many problems except adding variables to the jlabel extension class and forgetting to add them to the XML simulation class and subsequent replication routines
Therefore, it would be great if I could create a class with additional data declarations (such as celldatarecord. Java) and use it in both places (jlabel extension and XML data) without having to use something like XML data. CellDataRecordXXX.
Solution
You can do this using inheritance or using interfaces, where variables are set to constants in the parent class Since you want to extend jlabel, you should implement the interface on two classes:
public interface MyInterface { int someint = 9; } public class MyClass1 extends JLabel implements MyInterface { //this class has access to `someint` } public class MyClass2 extends JLabel implements MyInterface { // also has access to `someint` }
edit
Since you want to be able to change the same variable from different classes, you must ensure that you do not change the copy and change the same variable, so you should use the volatile keyword on the variable to indicate to Java that all threads should check the value before updating
Now you need a separate class so that you can generate instances from other classes to get values You must use the static keyword to ensure that a copy is kept for all class instances
public class MyVariableWrapper { public static volatile int some_var = 9; public void updateSomeVar(int newvar) { some_var = newvar; } public int getSomeVar() { return some_var; } }
Now the other two classes do this:
public class MyClass1 extends JLabel { MyVariableWrapper myVariableWrapper; MyClass1() { super(); myVariableWrapper = new MyVariableWrapper(); // Now I have access to `some_var` } } public class MyClass2 extends JLabel { MyVariableWrapper myVariableWrapper; MyClass2() { super(); myVariableWrapper = new MyVariableWrapper(); // Now I have access to the same `some_var` as MyClass1 } // this is a wrapper method for your convenience // since you don't like the excess code when accessing the variable public int getSomeVar() { return myVariableWrapper.some_var; // or myVariableWrapper.getSomeVar(); } public void setSomeVar(int newvar) { myVariableWrapper.some_var = newvar; // or myVariableWrapper.setSomeVar(newvar); } }
Now you can do this:
MyClass2 myClass2 = new MyClass2(); System.out.println(""+myClass2.getSomeVar());