Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I have two classes share the same variable definitions

Tags:

java

oop

xml

swing

What I really need is to be able to declare regular variables in an interface and implement that interface in two classes that I would not have to have to re-declare these in each class (ie class.data.variables instead of class.variables). Is there any way that I could achieve the same goal differently?

To give more detail. Essentially, I have created a small drawing program that drops JLabels on a JPanel that is on a JScrollPane. Because I have a specific design for these JLabels (ie they are not just for drawing they represent airline objects for this application), I have a class that extends JLabel and adds my application specific variables to it. Ultimately, I read and write an XML file with these variables so they can load and save their designs. Since I can not use this extended class for my XML definitions because it screams about the parent class even though I told it to have NONE as the accessor (I read there is a bug), I have to create an identical class and copy values back and forth for saving and loading. Not too much of a problem except when I add a variable to the JLabel extended class and forget to add it to the XML mimic class and subsequent copy routines.

So, it would be great if I could make one class (say CellDataRecord.java) that held the extra data declarations and have that class be used in both places (the JLabel extension and the XML data) without having to have something like XML.data.CellDataRecordXXX.

like image 215
Patrick Aquilone Avatar asked May 09 '12 12:05

Patrick Aquilone


People also ask

Can two classes have same method?

Having two or more methods named the same in the same class is called overloading. It's not overloading if you have the same method name in two different classes.

Can two variables of different types have the same name?

Yes, if they are not in the same lexical scope. For example (we have two variables named x , one is the formal int x , and another is the double x in the inside block).

How do you pass a variable from one class to another?

You can access all the variables by using the subclass object, and you don't have to create an object of the parent class. This scenario only happens when the class is extended; otherwise, the only way to access it is by using the subclass.


2 Answers

You can do that with inheritance or using an interface, where the variable is set as a constant in the parent class. Since you are extending a JLabel, you should implement the interface on 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 have to ensure you aren't changing copies and are changing the same variable, so you should use a volatile keyword on the variable to indicate to java that all threads should check the value before it updates it.

Now you'll need to have a separate class so that instances can be made from other classes to get the value. You have to use the static keyword to ensure that one 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 just 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());
like image 80
Ozzy Avatar answered Oct 06 '22 01:10

Ozzy


I'm not sure I 100% grasp your problem but from the first few lines of your description, instead of implementing an interface, you could define an abstract class and have your classes extend it. That way, you'll be able to define attributes in the abstract class and these will be common to all subclasses.

like image 35
kyiu Avatar answered Oct 06 '22 00:10

kyiu