This content has been marked as final. Show 5 replies
Well generally you should be returning values in methods, rather than attempting to have the method actually change the value of a passed parameter.
Then please, could you tell me how to 'return values'? I just want the variable I passed into this method, to be changed by the end of the method so that I can call on this variable in other methods, and it will give me a value different from the original value.
Ex. If I made a variable called int temp = 0 and then passed it into said method, which changes the value to temp = 4, I want the value of temp to remain at 4 and not return to 0 when I exit the method.
Perhaps i'm not explaining my question properly?
Perhaps i'm not explaining my question properly?You're explaining it properly, but the behavior you're looking for is deliberately not available in Java. Check uj's example; she has shown you how to get the desired result.
C/C++ used pointers to allow functions to change values that were passed in as parameters. This was a very powerful and flexible system, but very very dangerous. It is quite easy in C to accidentally overwrite data, or create memory leaks and dangling pointers that point to nowhere, or create all sorts of problems that are a nightmare to debug.
Any debugging problem you've had in Java pales in comparison to debugging a standard pointer error (/exaggeration). Java intentionally took away this flexibility to allow for more easily-readable code, less run-time errors, and the automatic garbage-collection saved us from a large majority of memory leaks.
Whether or not you understand what I said above...just know that you should be thankful that you need to make methods return a value.
just know that you should be thankful that you need to make methods return a value.Methods needn't return a value. Methods can manipulate objects by virtue of local copies of object references.