Damo42
Damo42

Reputation: 9

Can I change an int variable value using the output of an int method in java

I understand the concept of taking an int value into a method which is effectively a copy of the int value, rather than the actual value and memory location.

Is there a way of changing the main integer value based on the result of the method?

If I return the int value to main can I do something like int num = int numIn where int numIn is the returned value. If yes the can it be used for multiple methods returns, so that the main int value changes after each consecutive method return ?

Upvotes: 0

Views: 598

Answers (1)

Saj
Saj

Reputation: 18712

You must begin here.

int i = 10;

private int add(int x, int y){
    return x + y;
}

Initially the value of i is 10. But if you call-

i = add( 5, 10 );

The new value of i is now 15.

Upvotes: 2

Related Questions