0

I usually program in python and C, so this world of java confuses me quite a bit.

I am looking for a way to make my code more elegant, and in these other languages I would do it by passing by reference (or with pointers, depending on the language), the variable.

At one point in the code, I repeat a piece of code several times because each one depends on a certain previous situation. In summary, it could be understood that I want to modify several variables with the same code block, for which I first want to select the variable that I want to modify, assign it a "nickname", and then work with the nickname but modify the original variable.

So i want to change this

if (condition){
    if (condition){
        //modify var1
    } else {
        //modify var2
    }
}else if (condition){
    if (condition){
        //modify var1
    } else {
        //modify var2
    }
}

for something like this

if (condition){
    nickname = var1;
} else {
    nickname = var2;
}

if (condition){
    // modify nickname
}else if (condition){
    // modify nickname
}

Is there a way to do this in Java? Or am I condemned (using this aproach) to have redundant code?

In C i could do something like

// nickname as pointer from type of var1 and var2
if (condition){
    nickname = &var1;
} else {
    nickname = &var2;
}

// Work over nickname
8
  • 2
    Could you show an equivalent of this in C or Python? I don't understand what you want from the description. Or some pseudocode is also fine. Commented Mar 9, 2021 at 20:16
  • This is how you would do it in Java. It's fine. Commented Mar 9, 2021 at 20:17
  • 1
    You cannot pass references like in C. But in this case, you can extract the code to a method, return var1 or var2 and assign the method's return value to nickname. Commented Mar 9, 2021 at 20:17
  • @ublec When i do this the value of var don't change Commented Mar 9, 2021 at 20:26
  • @f1sh I get that, butI would prefer that when altering to "nickname", the variable to which it had been linked had already been changed Commented Mar 9, 2021 at 20:28

1 Answer 1

1

Given

if (condition){
    nickname = var1;
} else {
    nickname = var2;
}

This works ONLY if var1 and var2 are:

  1. References to object instances (i.e. not primitive values)
  2. Mutable, i.e. have internal state that can be changed (String is immutable)
  3. Type-compatible with each other. nickname must be assignable from both var1 and var2

In that case you can use nickname to modify the internal state of a referenced object.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, unfortunately this ends my search. I am working with String.
@bench The strings are immutable in java. So no matter how many references you have, you won't be able to modify them.
@bench Is your question actually answered at this point? I worry that you might think: "Changing a bunch of characters in Java is impossible.".
This probably won't fix it, you're right @akuzminykh. Is there any way to do something like this but with Strings. In my code I am concatenating them, so i know that i can modify theme, but I create several variables that store things that will behave in a similar way, that is why I am looking for this behavior but for Strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.