I've been programming with Java for a while and am still unsure about something pretty fundamental. I am using a GUI to call methods from different classes, which works fine. However, I'm having trouble sharing the variable values from one class to another. Here is an example which I hope makes things clearer.
I have two public classes Mouse and Cat, with methods executed from a separate class for the GUI. To execute methods from the two classes I creates object in the GUI:
And then run use these to run methods. Example Mouse method = run() and Cat method = chase():public Mouse eek = new Mouse(); public Cat meow = new Cat();
However, the problem is I have public variables in these classes that I want to share between them. My question is: What is the best way to do this? I knoweek.run(); meow.chase();
int numCheese = eek.cheese;
would get the value of cheese from Mouse and assign it to numCheese in the GUI class, but is this the best way or should I encapsulate with getter and setter methods? I want to be able to access Mouse variable values in Cat and visa-versa without having to make explicit copies in the GUI class if possible. It seems like I am going to have a lot of duplicate code in my GUI if I have to keep on copying all these variables in this way for each potential user input - there has to be a more efficient way to share variable information, right?
Hope all this makes sense, sorry for the annoying examples. Would really appreciate some advise before I start bloating my GUI code