0

I'm working on a calculator and I search how I can optimize my code.
The thing is that I have much code duplication due to if I'm working on the first number of the calculation or the second.
So I'm searching if it is possible to modify the value of an attribute sent in argument of a function ? (I think not because I saw nowhere the answer).

Maybe I'm expressing myself badly so here is a code below to explain what I'm talking about:

public class MyClass
{
    private static int number1 = 1;
    private static int number2 = 2;

    public MyClass()
    {
        changeValueOf(number1, 3);
    }

    private static void changeValueOf(int number, int value)
    {
        //Change here the value of the correct field
    }

}
RissCrew
  • 5
  • 1
  • 2
    It is not possible with primitives or immutable objects. It is possible with mutable objects. For an explanation please read [Is Java "pass-by-reference" or "pass-by-value"?](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value). But why do you want to change the parameters? Why not return a value from the method? – Turing85 Aug 01 '20 at 15:27
  • Because I'm not only just changing the value of my attribute, for example after changing the value, I need to get a custom String of the correct attribute and then render this String on my TextField – RissCrew Aug 01 '20 at 15:35
  • Can you show an example of the duplication you want to solve, so we can suggest an alternative? – Joni Aug 01 '20 at 15:45
  • Based on @Turing85’s answer, if your argument is a primitive, you can wrap it in a class. The wrapper must have setters (i.e. be mutable). In your method, you can mutate your object and alter any other application state you want. Do make note though that mutations come at their cost; [here](https://stackoverflow.com/questions/30665622/why-do-we-need-to-avoid-mutations-while-coding-what-is-a-mutation) is a good thread to read. – x sylver Aug 01 '20 at 15:47
  • @Joni you can find in this pastebin, the attributes I use and the part of the second where the duplication is (if you need more of the code ask it): https://pastebin.com/3d5xt1FL – RissCrew Aug 01 '20 at 15:52
  • Thx for the threads @xsylver & Turing85 ! But now I just realized that I'm using Double and not double as type of my variables (cause I need it to be null sometimes), Double (not double) is also considered as a primitive type ? If yes I think I'm going for a wrap then – RissCrew Aug 01 '20 at 15:56
  • Double is an immutable class already (all primitive wrappers are). You can try to go for your own wrapper over `double`. – x sylver Aug 01 '20 at 16:04

2 Answers2

2

First of all, you can modify static variables inside the method:

private static void changeValueOf(int value)
{
    number1 = value;
}

But I guess that is not what you a looking for :)

In Java (and in most other languages) primitive data type (int, short, long, etc) passed by value, e.g. the copy of value passes to the method (function). And reference types (objects, e.g. created with new operator) passed by reference. So, when you modigy the value of reference type (object) you can see the changes in the outer scopes (for example, in method caller).

So, the answer is no - you cannot change the value of int so that the outer scope would see the updated value.

Howewer, you could wrap your int values with some object - and it change the value inside of it:

public class Example {
    public static void main(String[] args) {
        Example app = new Example();

        // Could be static as well
        Holder val1 = new Holder(1);
        Holder val2 = new Holder(2);

        app.changeValue(val1, 7);

        System.out.println(val1.value); // 7
    }

    public void changeValue(Holder holder, int newValue) {
        holder.value = newValue;
    }

    static class Holder {
        int value;
        Holder(int value) {
            this.value = value;
        }
    }
}

Also, you could create an array with 2 values and update them inside the method, but it's not very good approach IMO

And finally, you could just return updated value and assign it to your variables:

public class Example {
    private static int number1 = 2;
    private static int number2 = 3;

    public static void main(String[] args) {
        Example app = new Example();
        
        number1 = app.mul(number1, 7);
        number2 = app.mul(number2, 7);

        System.out.println(number1); // 14
        System.out.println(number2); // 21
    }

    public int mul(int a, int b) {
        return a * b;
    }

}
Nikita
  • 194
  • 10
1

One possibility is to use an array to store your variables, instead of separate variables with numbers affixed. Then you would write number[1] instead of number1 for example. You can pass the array index number around to indicate which variable you are referring to.

public class MyClass
{
    private static int[] variables = {1, 2};

    public MyClass()
    {
        // change value of first variable
        changeValueOf(0, 3);

        // now variable[0] = 3
    }

    private static void changeValueOf(int number, int value)
    {
        variables[number] = value;
    }

}
Joni
  • 108,737
  • 14
  • 143
  • 193