• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to pass an object 'byvalue'  RSS feed

 
utsav gupta
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
I want to pass an object to function such that a local copy of that object is made in that function and the original object remains unchanged.

Can anybody help?

Thanks
Utsav
 
Vivek Singh
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you will pass an object then it will be pass by reference. Not pass by value (like yoou have passed a primitive type)



the output will be:-
10
10
30
10
 
utsav gupta
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Thanks for your reply.
I know that an objet will be passed by refernece as you mentioned.
My question is I dont want it to pass by refernce but by value to the function so that the fields of the original object could not be changed from the function.
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
utsav gupta wrote:
I want to pass an object to function such that a local copy of that object is made in that function and the original object remains unchanged.


There's no such parameter passing mechanism in Java. Instead you have to actively create a copy of the object, either before you pass it to the method or in the method after you've passed it.

There are essentially two ways of copying an object. Either you supply a copy constructor or implement Cloneable.

http://en.wikipedia.org/wiki/Clone_(Java_method)

http://java.sun.com/developer/JDCTechTips/2001/tt0306.html
 
Vivek Singh
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
uj nossnahoj wrote:
utsav gupta wrote:
I want to pass an object to function such that a local copy of that object is made in that function and the original object remains unchanged.


There's no such parameter passing mechanism in Java. Instead you have to actively create a copy of the object, either before you pass it to the method or in the method after you've passed it.

There are essentially two ways of copying an object. Either you supply a copy constructor or implement Cloneable.

http://en.wikipedia.org/wiki/Clone_(Java_method)

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


We can make a proxy object also.
 
utsav gupta
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for all your suggestions.

Though I still wish there would have been a simple pass by value option for objects.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is pass-by-value. You pass the value of the reference. You mean you would like all objects to be passed by copy of the object.
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
utsav gupta wrote:thanks for all your suggestions.

Though I still wish there would have been a simple pass by value option for objects.


Sure that would be handy. The reason why Java doesn't allow it is the so called "slicing problem".

Slicing happens when an object of a certain type is copied to a variable of the supertype. Then the subtype part of the object is sliced off in the copying process. This is what would happen if an object could be passed by value to a method because this kind of parameter passing involves copying of the object.

So to avoid problems with slicing, Java handles all objects via references. References are never sliced when copied.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!