• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

question concerning clone() method

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as mentioned earlier

Cloning means creating a new object based on another object. As said, they are 2 different objects that share the same value.



when object B is cloned from object A.
then later after the execution of the programme, object A changes its properties or behaviour or both. My question is, will object B change too?

Thanks

Sura
 
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

i guess changes in object A will automatically be reflected in object B. since B is a cloned instance of A.

others plz comment?
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sura watthana:
when object B is cloned from object A.
then later after the execution of the programme, object A changes its properties or behaviour or both. My question is, will object B change too?



Yes. clone() performs a shallow copy, meaning only object references are copied but these object references in objects A and B both point to the same objects.
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello,

technically the answers are correct. But clone() should be implemented in such a way that changes in one object are not reflected by the other one. A typical way to override Object#clone() is to call super.clone() and then "fix" those fields that would otherwise cause this reflection.



Recommended reading: Effective Java, Chapter 3

Conan
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
correct. Clone() SHOULD make a deep copy of your object, but the compiler cannot do that on its own so only copies object references and not the referenced objects.

To put it simply, a deep copy is made of the references when what you'd want is to create new references to clones (however deep it goes) of all referenced objects.
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Is clone() covered in SCJP???

May be I have not gone through this topic, could you tell me, to which topic it is concerned???

Thanks
Kap
 
sura watthana
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes clone() is covered in SCJP exam.
 
sura watthana
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, method clone() is covered in SCJP 1.4 exam.
The topic concerned is the "Object" class.

hope this helps
Sura
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never got a clone question in the Dan Chisholm mocks. Never saw the topic covered in the K&B book. Never saw the question on the exam.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Robert's correct!

You can relax on this one, the SCJP 1.4 exam does NOT cover the clone() method!

Yeah! Of course, it's good to understand clone(), but if you're just focusing on the exam you can put your precious study time to use in other areas. The areas that are typically the best places to put your emphasis are:

THREADS
INNER CLASSES
GARBAGE COLLECTION

There will be lots of questions on these topics, and they're pretty tricky!

Someone might do a search on an old topic from this forum entitled something like:

"Top ten things people think are on the exam, but aren't"

If you find it, resurrect it!

Thanks,

Bert
 
kapil munjal
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey friends,

check this out..........

Top ten to top 100%

well, I found it through search on this forum......

Thanks Bert.....
 
Who knew that furniture could be so violent? Put this tiny ad out there to see what happens:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic