How can I use two classes with the same variable definitions

I really need to be able to declare regular variables in an interface and implement this interface in two classes that I would not need to re-declare in each class (e.g. class.data.variables instead of the .variables class). Is there a way I could achieve the same goal in a different way?

More details. Essentially, I created a small drawing program that outputs JLabels to a JPanel that resides on a JScrollPane. Since I have a specific design for these JLabels (i.e. they are not just for drawing, they are airline objects for this application), I have a class that extends JLabel and adds my application variables to it. Ultimately, I read and write an XML file with these variables so that they can load and save their projects. Since I cannot use this extended class for my XML definitions because it screams about the parent class, although I told it that it has NONE as an accessor (I read that there is an error), I need to create an identical class and copy the values ​​back and forward to save and load. There are not too many problems, except when I add a variable to the extended JLabel class and forget to add it to the XML mimic class and subsequent copy routines.

So, it would be great if I could create one class (say, CellDataRecord.java) that contained additional data declarations, and that this class is used in both places (JLabel extension and XML data) without having to have something like XML.data.CellDataRecordXXX.

+6
source share
2 answers

You can do this with inheritance or using an interface where the variable is set as a constant in the parent class. Since you are extending JLabel, you must implement an interface for both 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 need to make sure that you do not change copies and change the same variable, so you should use the volatile keyword for the java variable, so that all threads must check the value before its updates.

Now you need to have a separate class so that instances can be made from other classes to get the value. You must use the static to ensure that one copy is stored for all instances of the class.

 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 two other classes just do it:

 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()); 
+5
source

I'm not sure that I will 100% catch your problem, but from the first few lines of your description, instead of implementing an interface, you can define an abstract class and extend its classes. This way you can define attributes in an abstract class, and they will be distributed in all subclasses.

+1
source

Source: https://habr.com/ru/post/915283/


All Articles