Solved

What is the keyword for modifiable method parameter in Java?

Posted on 2009-07-02
9
342 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

730 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