aspose file tools*
The moose likes Java in General and the fly likes java passing variables by reference? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "java passing variables by reference?" Watch "java passing variables by reference?" New topic
Author

java passing variables by reference?

Nikos Stavros
Ranch Hand

Joined: Feb 24, 2006
Posts: 243

is there a way to do this in java, like the following c++ code


Jesus lives
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
Not the way you coded it. If you want to manipulate a bunch of int primitives and return the values, pass an array of int (a reference to an array object.)
Bill


Java Resources at www.wbrogden.com
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
As you are using a single method to process three ints, then we can assume those three ints are related in some way. If that is the case then they should be in a class.


This class should have a method called duplicate (double would be a better name) that doubles the value of each of the ints


Then your main method looks like this

If the three ints are not related in any way, then it is probably a bad design to have a single method processing them.
[ August 15, 2006: Message edited by: Joanne Neal ]

Joanne
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
No you can't do that. Everything in Java is passed by value.


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
While William's solution using an array would work, I find it to be a rather awkward workaround.

A goal of OO design is to put the data, and the operations on the data together in a class. Joanne's solution is one of the possible ways to do that.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: java passing variables by reference?
 
Similar Threads
Passing objects by reference or value???
Chapter 5 K&B -Doubt
doubt on this question(from a Mock Test)
Nested for loop
Doubt In Kathy& Bates book questions at page 198