What is the keyword for modifiable method parameter in Java?

Hello experts,

  An easy question from a newbie to Java. In C sharp, I use the keyword ref or out
when one method parameter is not a copy but the true object passed to the method.
How does it work in Java? PLease post a snippet of code.

Thank you!
Who is Participating?
objectsConnect With a Mentor Commented:
following explains how java passes arguments by value


If you don't want to allow the value passed to be changed then declare the arg as final

public void doSomething(final int a) // a cannot be changed
All method parameters come by reference,
unless those are simple values, then those are copies.
There is no keyword for it,
and a ReadOnly parameter doesn't exist in JAVA.

All method parameters are passed by *value*. There is no pass by reference in Java
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

If the parameter is a "primitive variable" (int, byte, long, basically any lowercase type) then Java automatically passes it by value. This cannot be changed.

If the parameter is a "object variable" (JFrame, Integer, basically any uppercase type) then Java automatically passes it by reference. This cannot be changed.

If you need to pass an integer by reference, you can make an object to wrap the integer around.
>>then Java automatically passes it by reference. This cannot be changed.

It doesn't. It passes it by value. If it did, you *would* be able to change it - that's the definition of 'by reference'. It *is* a reference, but it's a copy of one.
pascal_lalondeAuthor Commented:
CEHJ your answer is right but imcomplete. objects gave a complete answer and easy to understand for a newbie. I mentionned I am a newbie and that I wanted a snippet of code. Thank you both.
pascal_lalonde, can you tell us why you ignored all comments there but one (part of which didn't even seem relevant)?
pascal_lalondeAuthor Commented:
Hello CEHJ,

  I ignored the comments from sciuriware and Thomas4019 because they contain an error. No method parameters are passed by reference in Java. Then only your comments and those from object remain.
I indicated above how I discriminated between both of you. The answer if fully relevant anc complete.
I wanted to give points for a full answer not a partial and fast answer.

glad I could help :-)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.