Solved

What is the keyword for modifiable method parameter in Java?

Posted on 2009-07-02
9
344 Views
Last Modified: 2012-05-07
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!
0
Comment
Question by:pascal_lalonde
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 24

Expert Comment

by:sciuriware
ID: 24767236
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.

;JOOP!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24767480
All method parameters are passed by *value*. There is no pass by reference in Java
0
 
LVL 17

Expert Comment

by:Thomas4019
ID: 24767736
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 24768151
>>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.
0
 
LVL 92

Accepted Solution

by:
objects earned 75 total points
ID: 24769023
following explains how java passes arguments by value

http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html
http://www.yoda.arachsys.com/java/passing.html

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
0
 

Author Closing Comment

by:pascal_lalonde
ID: 31599333
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24772643
pascal_lalonde, can you tell us why you ignored all comments there but one (part of which didn't even seem relevant)?
0
 

Author Comment

by:pascal_lalonde
ID: 24785754
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.

Regards.
0
 
LVL 92

Expert Comment

by:objects
ID: 24790137
glad I could help :-)
0

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

687 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question