-1

I confirmed my initial thoughts that in Java a variable can only be changed if specifically told to change:

public class VariableChangeTest {
    public VariableChangeTest() {

    }

    public static void main(String[] args) {
        int first = 1;
        int second = 2;
        int third = first + second;

        System.out.println("first = " + first); /* first = 1 */
        System.out.println("second = " + second); /* second = 2 */
        System.out.println("third = " + third); /* third = 3 */

        second += first;

        System.out.println("********************");

        System.out.println("first = " + first); /* first = 1 */
        System.out.println("second = " + second); /* second = 3 */
        System.out.println("third = " + third); /* third = 4 */

        first += third;

        System.out.println("********************");

        System.out.println("first = " + first); /* first = 5 */
        System.out.println("second = " + second); /* second = 3 */
        System.out.println("third = " + third); /* third = 8 */
    }
}

In this example, the comments reflect I want to happens, not what actually happens. What actually happens is that third always stays at what first + second originally was, that is, 3. What I want is to be able to assign a variable to the current value of one or more other variables, so that it updates if one of its right-hand assignment values changes. Is this possible in Java?

UnworthyToast
  • 825
  • 5
  • 11
  • 21
  • 1
    no, since java is `pass-by-value` only. The only thing you could do is creating a Wrapper type class and use methods that dynamicly calculate the result. But working with primitivies only you wont be able to do this. – SomeJavaGuy May 09 '16 at 14:33
  • Do Java's built-in wrappers (Double, Integer, etc) have this functionality? – UnworthyToast May 09 '16 at 14:37
  • no they don´t have it. What i meant was something like creating your own calculator class that does return the result. But you can´t work with the reference of the primitive in java, which is what you are looking for. – SomeJavaGuy May 09 '16 at 14:37
  • Is this question merely for learning, or do you have a specific use case in mind? As @KevinEsche noted, there are potential approaches for dynamically calculating a value, but it relies upon creating some particular class. What are you hoping to achieve? – KevinO May 09 '16 at 14:39

2 Answers2

4

What you can do is use getters. e.g. You could have

class A {
    private int first, second;

    A(int first, int second) {
       this.first = first;
       this.second = second;
    }
    public int getFirst() { return first; }
    public vodi setFirst(int first) { this.first = first; }
    public int getSecond() { return second; }
    public void setSecond(int second) { this.second = second; }
    public void addSecond(int x) { this.second += x; }
    public int getThird() { return first + second; }
    // a possible implementation
    public void setThird(int third) { second = third - first; }
    public void addThird(int x) { this.first += x/2; this.second += x - x/2; }
}

public static void main(String[] args) {
    A a = new A(1, 2);

    System.out.println("first = " + a.getFirst()); /* first = 1 */
    System.out.println("second = " + a.getSecond()); /* second = 2 */
    System.out.println("third = " + a.getThird()); /* third = 3 */

    a.addSecond(a.getFirst());

    System.out.println("first = " + a.getFirst()); /* first = 1 */
    System.out.println("second = " + a.getSecond()); /* second = 3 */
    System.out.println("third = " + a.getThird()); /* third = 4 */

    a.addThird(3);
    System.out.println("first = " + a.getFirst()); /* first = 2 */
    System.out.println("second = " + a.getSecond()); /* second = 5 */
    System.out.println("third = " + a.getThird()); /* third = 7 */

When ever you getThird() it will always runs code. This is part of the reason for having getters instead of accessing the fields directly. i.e. the field might not be as trivial as you might imagine and you don't want to update all the code which uses it, just change it in one place.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
2

Variables are for holding values, not performing calculations. If you want to dynamically derive a value from some other values, write a method:

private static int calculateResult(int a, int b) {
    return a + b;
}

public static void main(String[] args) {
    int first = 1;
    int second = 2;

    System.out.println("first = " + first); /* first = 1 */
    System.out.println("second = " + second); /* second = 2 */
    System.out.println("result = " + calculateResult(first, second)); /* result = 3 */

    second += first;

    System.out.println("********************");

    System.out.println("first = " + first); /* first = 1 */
    System.out.println("second = " + second); /* second = 3 */
    System.out.println("result = " + calculateResult(first, second)); /* result = 4 */

    // etc.
}
Wyzard
  • 33,849
  • 3
  • 67
  • 87