Home arrow static arrow Java Programming [Archive] - Call By Reference
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Call By Reference
This topic has 19 replies on 2 pages.    1 | 2 | Next »

Posts:106
Registered: 8/30/01
Call By Reference  
Aug 9, 2004 3:19 AM



 
Dear Friends,

I have a problem. I know that Java passes a copy of an object or a variable to the method, so actually the original object won't be changed.

I am developing a system to solve timetable problem, and when I pass an object Timetable to a method to solve it, I realize the the original object is changed and this is what I dont want t to happen:

public void solveTimetable(Timetable tt) {

tt.scheduledEvents = SolutionStatic.initEvents(tt);
showResults(tt) ;
}

The above simple code assumes that the original Timetable object Wont change, but in my case it does, I hope some one can help.

Note: the timetable object is composed of a set of objects I don't know if this has an affect.

regards
 

Posts:964
Registered: 7/23/03
Re: Call By Reference  
Aug 9, 2004 3:21 AM (reply 1 of 19)



 
you need to clone the object and only do changes on the cloned object...
 

Posts:18,384
Registered: 21.03.00
Re: Call By Reference  
Aug 9, 2004 3:23 AM (reply 2 of 19)



 
Hi,

It's only the argument that is passed by value, so you can't change the argument, but you can change anything that the argument references (as long as the attributes aren't final). There is no way around that. You could however implement clone, or a copy constructor for the class.

/Kaj
 

Posts:5,965
Registered: 5/17/03
Re: Call By Reference  
Aug 9, 2004 3:26 AM (reply 3 of 19)



 
I have a problem. I know that Java passes a copy of an
object or a variable to the method, so actually the
original object won't be changed.

This assumption is wrong. It's the variable that's passed by value and in the case of an object this variable contains a reference (pointer) to the object and not the object itself. So the reference is copied and not the object value.

I am developing a system to solve timetable problem,
and when I pass an object Timetable to a method to
solve it, I realize the the original object is changed
and this is what I dont want t to happen:

The above simple code assumes that the original
Timetable object Wont change, but in my case it does,
I hope some one can help.

You'll have to make a copy of the object value and operate on that.

Note: the timetable object is composed of a set of
objects I don't know if this has an affect.

It matters because you may have to make a deep copy which means you have to make copes of all objects held by Timetable.
 

Posts:11,200
Registered: 7/22/99
Re: Call By Reference  
Aug 9, 2004 3:26 AM (reply 4 of 19)



 
I have a problem. I know that Java passes a copy of an
object or a variable to the method, so actually the
original object won't be changed.
You are wrong: Java passes a copy of the object reference. The copy points to the same object, so the actual object can be changed while the reference that is passed can't be changed. Here's an example of passing array objects:
// won't work; tries to change the referencevoid expand(int[] array) {    array = new int[2*array.length + 1];} // will work; changes the referenced object void zero(int[] array) {    for (int i = 0; i<array.length; i++)         array[i] = 0; }
 

Posts:5,965
Registered: 5/17/03
Re: Call By Reference  
Aug 9, 2004 3:38 AM (reply 5 of 19)



 
A clone makes a shallow copy and this may be enougth if you're only changing variables held in Timetable. But if you change variables of objects held by Timetable then you must make clones of these too to be sure you're working on a copy.

Sometimes you can keep track of all this copying but it can be quite complicated to keep track of everything, and it's error prone. In that case it may be better to sovee it once and for all and make a general deep copy.

http://java.sun.com/developer/JDCTechTips/2001/tt0410.html
 

Posts:196
Registered: 6/24/97
Re: Call By Reference  
Aug 9, 2004 3:39 AM (reply 6 of 19)



 
Your Timetable class must implement the clone method - a method that makes a copy of the object itself.

The clone method is responsible for creating a new instance of hte timetable object and making a copy of each of the instance variables in the timetable class.

eg: If your timetable class consists of 3 strings a,b,c and a vector v:

public class Timetable {   String a,b,c;  Vector v;   public Timetable clone() {    // return a copy of self    Timetable toRet = new Timetable();    toRet.a = this.a;  // strings are immutable so a simple assignment will do    toRet.b = this.b;    toRet.c = this.c;    toRet.v = v.clone();   // vectors are mutable, so we need to get a clone of the vector.    return toRet;  }}


The important point here is that instance variables that are mutable (whose state can be changed), must be cloned as well.

So, in easy steps:
a) Write a clone method
b) For each instance variable in your class do steps c- g
c) Determine if the instance variable's class is mutable or immutable. For std Java classes a look at the javadoc should give you an idea.
d) If the instance variable is of an immutable class simply assign
e) If the instance variable is mutable, check if the class implements Cloneable
f) If the class implements cloneable create a clone and assign.
g) If the class does not implement cloneable you need to write some code to create a "deep copy" of the instance variable

Most introductory books on Java should cover cloneing in better detail and better english.

Post back if you need clarifications
 

Posts:106
Registered: 8/30/01
Re: Call By Reference  
Aug 9, 2004 4:07 AM (reply 7 of 19)



 
Thank you all for your help, I am wondering does this mean that in my Timetable Object where it has several object variables like Student, TeachingEvent, Room, etc... does this mean that each one of those objects should also implement the clonable interface, and override its clone method?
or just insure that they are cloned in the Timetable clone method i.e.
public Object clone() {

Timetable tb=null;
try {

tb = (Timetable)super.clone();
tb.rooms = (Room[])rooms.clone();
tb.students = (Student[])students.clone();
tb.availableTimeRoomPairs = (Vector)availableTimeRoomPairs.clone();
tb.usedTimeRoomPairs = (Vector)usedTimeRoomPairs.clone();
return tb ;

}
catch (Exception e) { }
return tb ;

}
 

Posts:106
Registered: 8/30/01
Re: Call By Reference  
Aug 9, 2004 4:21 AM (reply 8 of 19)



 
I believe it is much bigger story than I thought, there is a lot of talks about it one the web, I wonder why Java doesn't provide a method, similar to clone that easily perform this tasks???
 

Posts:21,718
Registered: 98-02-20
Re: Call By Reference  
Aug 9, 2004 4:37 AM (reply 9 of 19)



 
Java does provide the Cloneable interface, but the method isn't written for you because it's not easy to do for hierarchies.

%
 

Posts:161
Registered: 8/7/97
Re: Call By Reference  
Aug 9, 2004 4:37 AM (reply 10 of 19)



 
I believe it is much bigger story than I thought,
there is a lot of talks about it one the web, I wonder
why Java doesn't provide a method, similar to clone
that easily perform this tasks???

I think because it would be to dangerous. An automatic deep copy could copy just a few bytes but also hundreds of megabytes depening on the references. This way ppl have to think about their classes when they implement clone(). Just image that you clone an object that references a servlet session (assuming that the servlet engine implementation sucks and allows that you clone its data): you would probably clone the whole servlet engine
 

Posts:106
Registered: 8/30/01
Re: Call By Reference  
Aug 9, 2004 4:56 AM (reply 11 of 19)



 
I was trying to clone the Timetable Object just to enhance the performance, i mean I didn't want to load the whole data every time from a file, run the scheduler, eveluate, and then load again, ..., but in this case I believe it would easier for me just to live with IO overhead, rather than making a copy of the timetable. we have always to sacrify some thing -trade off-
 

Posts:196
Registered: 6/24/97
Re: Call By Reference  
Aug 9, 2004 5:42 AM (reply 12 of 19)



 
Thank you all for your help, I am wondering does this
mean that in my Timetable Object where it has several
object variables like Student, TeachingEvent, Room,
etc... does this mean that each one of those objects
should also implement the clonable interface, and
override its clone method?

Ideally, yes.

But in reality, you may decide to only make a clone of the objects that you are modifying - thats a tradeoff between a real clone, performance and development time.

I'dd recommend that you go back to the drawing board and see if you can come up with a solution that does not require cloneing
 

Posts:458
Registered: 7/1/03
Re: Call By Reference  
Aug 9, 2004 8:47 AM (reply 13 of 19)



 
Dear Friends,

I have a problem. I know that Java passes a copy of an
object or a variable to the method, so actually the
original object won't be changed.

Wrong. Java Passes by Value ALWAYS. However every variable to a constructed type (child of java.lang.Object) is a reference. Therefore you CAN change the contents of the object.

Look at chapter one of my book "Hardcore Java" http://www.oreilly.com/catalog/hardcorejv
or at Josh Bloch's book "Effective Java." http://java.sun.com/docs/books/effective/

Both explain this concept, though I do it in a bit more detail than Josh.
 

Posts:24,036
Registered: 2/3/03
Re: Call By Reference  
Aug 9, 2004 8:49 AM (reply 14 of 19)



 
This may help as well...

[url=http://www.javaranch.com/campfire/StoryPassBy.jsp]JavaRanch Campfire Stories: Pass-By-Value[/url]
[url=http://www.javaranch.com/campfire/StoryCups.jsp]Cup Size -- a story about variables[/url]
 
This topic has 19 replies on 2 pages.    1 | 2 | Next »