+ 6
java is not passing values to main method
hello, good morning/afternoon/evening/night. check this code: https://code.sololearn.com/c53giTIx9zWE/#java Does anyone know why the value is not passing? main method and static method is not syncing/passing values. the value IS passing from the MAIN method to the CLASS method but it is NOT passing from the CLASS method to the MAIN method... Thank you... if anyone knows how to solve it, please share it :) have a great morning/afternoon/evening/night. happy coding!
5 Réponses
+ 5
@Tibor Sanata
do you mean like this?
https://code.sololearn.com/c87zi9Vweqf9/#java
+ 4
integer types are value based not reference based. Therefore if they get updated in a function-scope the update won't be passed to the global variable.
+ 3
Mr. 🍊range Tibor meant like this:
https://code.sololearn.com/cLm7OG7vJ6jZ
but that doesn't solve the problem how you wanted it :(
🌟(Pro)metheus 🇸🇬 I just googled for an answer to op, and it says java always passes by value, no matter the type. That's very sad :(
.
+ 1
You can declare your class variables outside of the main method (just after class testing). They need to be static too, as your methods, because you are not instantiating your testing class. In that case you don't even need to pass them to your add method as parameters, because the method can directly access and modify static class members (variables).
+ 1
Mr. 🍊range You should read about pass by value and pass by reference. It will give you a better understanding.