• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

How to Assign an object to another of the same kind?

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java doesn't mark a compilation error,
but it seems not to accept this assignment:

Object2 = Object1;

Provided that Object1 and Object2 are of the same kind with one array and one int variable inside them, and both declared as TestClass.
The code runs. but results shows the assignment above fails.

Is there a "formal way" of doing this assignment?
(I'm trying to avoid assigning item by item from inside objects)

Thanks in advance.

From Venezuela:
Wish you all a Merry Christmas
and A Happy New Year!!!
 
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am beginner but i will give my thoughts about your question and see if i am correct, as i am sure others will correct me if i am wrong.

I don't think it is possible to make one object another as they are 2 completely different references of the same class.
I believe you could compare them but not make one become another.
 
Sergio Campos J.
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks mike!
Actually, assigning item by item is cheap here.
But in a more complex construction it could be quite expensive.
That's why I'm sure there should be a way of doing this.
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sergio,

Like i said i am new so it is completely possible i am wrong(or even probable ;)....)
 
lowercase baba
Posts: 13054
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what do you mean by "results shows the assignment above fails"? Can you show us your code?

I'm not sure, but you may be talking about what's called a "deep copy" vs. a "shallow copy". Try googling those terms and see if that helps.
 
Sergio Campos J.
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:[...] google "deep copy" [...] see if that helps.


It helped, Yes! Thank you!
TestClass Object2 = (TestClass) Object1.clone();

I think I was overwriting Object1 (shallow copy) which was supossed to stay unaltered
 
fred rosenberger
lowercase baba
Posts: 13054
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad i was able to point you in the right direction.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic