• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to pass an object 'byvalue'

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic