5

I realize this is probably a really basic question, but I can't figure it out.

Say I have this main class

public class Main{ public static void main(String[] args){ int a = 0; AddSomething.addOne(a); System.out.println("Value of a is: "+String.valueOf(a)); } } 

Here is AddSomething class and addOne() method

public class AddSomething{ public static void addOne(int a){ a++; } } 

The addOne method is not adding anything

System.out.println("Value of a is: "+String.valueOf(a)); // Prints 0 not 1 

How can I make Add class update variable a in Main class?

4
  • Java is call-by-value. Commented Nov 27, 2015 at 7:30
  • 1
    check stackoverflow.com/questions/3330864/… Commented Nov 27, 2015 at 7:30
  • Although someone give you an alternative method to do what you want, but it's not actually add one to a, it assign an new value to a. So I suggest you to read some book that introduce JAVA and see what JAVA's Object and primitive types are passing during function call. Commented Nov 27, 2015 at 7:33
  • Maybe this post can help you understand better what is happening : stackoverflow.com/questions/40480/…. Java is "pass by value" and I think it is an important thing to understand. Commented Nov 27, 2015 at 7:35

2 Answers 2

6

addOne receives a copy of a, so it can't change the a variable of your main method.

The only way to change that variable is to return a value from the method and assign it back to a:

a = Add.addOne(a); ... public int addOne(int a){ return ++a; } 
Sign up to request clarification or add additional context in comments.

14 Comments

This is true for a static method. For completeness, it would be better to explain what problems this may cause and what are alternatives (ie. creating instance etc.)
So there is no way to pass the object without making a new copy? I thought I could get around this using static classes somehow.
@the_prole You can pass to addOne an instance of a class having an int property, and addOne would be able to mutate the int property of that instance.
@Eran so primitive types pass by value, but instances pass by reference?
@the_prole Both are pass by value, but for reference types (i.e. variables the refer to instances of classes), if they refer to a mutable object, you can change the state of that object inside the method. You can't change the reference itself to refer to a different object.
|
2

Thats becouse primitive types in java pass to methods by value. Only one way to do what you want is reassign variable, like :

public class Main{ public static void main(String[] args){ int a = 0; a = Add.addOne(a); System.out.println("Value of a is: "+String.valueOf(a)); } } 

and

public class AddSomething{ public static int addOne(int a){ return a++; } } 

3 Comments

I see, so primitive type pass by value. How about ArrayLists?
@the_prole it's an object, so it's will pass-by-reference. Yep. You can modify it.
Beware that it is a bit tricky to say "by value" or "by reference". Technically everything is passed "by value". In case of objects the value is the reference value, i.e. the object address in memory. This is important to understand, because you can't change the object, i.e. you can't replace your ArrayList (that came as a method parameter) with another ArrayList, you can only mutate the object via its methods, for example, ArrayList.add().

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.