Shared variable for other classes
Solved the initial question, however posted a new one in the fourth post if you would bother to scroll down and see if you know the answer! :) Thank you!
Direct link to new question:
In my effort of trying to make my program more tidy and clean I've encountered a problem. Previously I've only coded my programs in one big mess, so now I'm trying to split it up into separate classfiles.
I'm trying to make a simple paint program, where I'm trying to split it into classfiles as to what makes sense to me. I might have tried to do it in a very difficult way, and I appreciate any tips beyond my problem, the thing I can't quite figure out however is how I can efficiently share variables between my classes.
I've divided my program into the following java files:
- Contains only one line of code generating the GUI which is in a seperate file.
- Contains the GUI and actionlisteners, as well as the mouselistener which I want to get the coordinates from.
- Contains information about what the user has chosen. Currently there is selection of shape, fixed sizes, fill or just the "edge" and color. This also contains the xStart, xEnd, yStart and yEnd variables which I want to pass from gui into the drawing file.
- Contains the functions for drawing.
The problem is however, when I do make a new instance of settings in each file they do not share variables obviously.
So when I in the gui class after having instanced
in both files, they do not share the same variables. This might be obvious, but I thought it was going to work :)
settings s = new settings();
I then do in the gui file a
for each of the variables. This ends up with the instance of settings in the gui file containing the right value but obviously not in the drawingPanel file.
How do I pass variables from one class file to another? Can I make like a public double xStart which all classes can access and edit?
Thank you for answers, and sorry if my english isn't that good. If anything is unclear please ask and I'll try to clarify.