wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes What is passing by value? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is passing by value?" Watch "What is passing by value?" New topic
Author

What is passing by value?

Jake Miller
Ranch Hand

Joined: Jun 27, 2007
Posts: 43
What does it mean that Java is pass by value?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

See this JavaRanch campfire story: Pass by value.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jake Miller
Ranch Hand

Joined: Jun 27, 2007
Posts: 43
Thanks, pretty helpful. Another question re:

"In line 1, a cup called x, of size int, is created and given the value 3.

In line 2, a cup called y, of size int, is created and given the value... 3.

The x variable is not affected!

Java COPIES the value of x (which is 3) and puts that COPY into y. "

So in Java, if I type int x = 3, int y = x, it COPIES whatever x is and puts it into y. It's as if I right clicked on x and pasted it to y, right? How would I have y reference x, as in if I changed x it would also change y? (Like in excel when you reference another cell and change that cell...)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Jake Miller:
...How would I have y reference x, as in if I changed x it would also change y? (Like in excel when you reference another cell and change that cell...)

That's a good question.

You cannot do this with Java primitive types, because the value itself is simply copied to the new variable. But if you want y to change whenever x changes, then maybe you don't need y at all. Just use x.

A more elaborate approach would be to replace y with a method that "gets" the current value of x when needed. This might be appropriate if x is an instance variable of some other object.
Jake Miller
Ranch Hand

Joined: Jun 27, 2007
Posts: 43
Originally posted by marc weber:

But if you want y to change whenever x changes, then maybe you don't need y at all. Just use x.



Oh yea. You could do that huh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is passing by value?
 
Similar Threads
BigDecimal: divide round help
tomcat not running??
Doubt on EL
Doubt in body-content value
Socket connection problem