Can you link two or more variables so that if you change the value of one the rest change to the same value as well?
I thought of making a method that does that but I wanna know if there is an easier way of doing this.
Can you link two or more variables so that if you change the value of one the rest change to the same value as well?
I thought of making a method that does that but I wanna know if there is an easier way of doing this.
I think this can help you:
JFrame a = new JFrame("A");
JFrame b = new JFrame("B");
a = b = new JFrame("C");
System.out.println(a.getTitle() + " - " + b.getTitle());
a.setTitle("D");
System.out.println(a.getTitle() + " - " + b.getTitle());
You assign same instance to multiple variables and after that change only one.
I used JFrame just as example.
P.S.: This works only for objects. For primitives(byte, short, int, long, float, double, boolean, char and String
) doesn't. If you want to work with primitives, you need to create an Class which has that primitives as fields and change them via object.