?
Solved

What is the keyword for modifiable method parameter in Java?

Posted on 2009-07-02
9
Medium Priority
?
352 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 300 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

771 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