I've heard from many people that Java is a pass-by-value language. If so, how can a set function change a variable's value?
Asked
Active
Viewed 172 times
-2
-
It uses the method argument to change a field in the object. There is no need to change the argument itself. – VGR Jun 21 '17 at 14:37
-
Then isn't there any way to change a private variable inside a class? – Ahmet Emre Boyacı Jun 21 '17 at 14:37
-
3A private variable can be accessed and changed inside that Class. – Felix Jun 21 '17 at 14:39
-
does java have a pass by reference? if so thats what you need, never used java so cant confirm if java includes this – WhatsThePoint Jun 21 '17 at 14:50
-
@WhatsThePoint [no](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value). – Andy Turner Jun 21 '17 at 14:53
-
but,guys, the thing that confused me was that I thought a function couldn't change the actual value if the variable wasn't in its scope. But when I use a set function to change the value of an int and get it with a get function, i get the new value. how so? – Ahmet Emre Boyacı Jun 21 '17 at 14:56
1 Answers
-1
In Java we use accessors to accomplish this.
See : https://stackoverflow.com/a/1568230/967768
and this is a good reference to understand pass-by-value and how to test for it.

Demogorii
- 656
- 5
- 16
-
Thanks a lot for the fast reply, it really helped me understand! – Ahmet Emre Boyacı Jun 21 '17 at 14:44
-
@AhmetEmreBoyacı you can mark this as the answer if it helped. Thanks! – Demogorii Jun 21 '17 at 15:24