Solved

What is the keyword for modifiable method parameter in Java?

Posted on 2009-07-02
9
334 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java 1603 Error 3 50
JList custom Cell Renderer refresh 15 53
Spring Framework HTTPSession management 1 24
javap bin 2 30
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

863 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

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now