• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

clone method

 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What exactly clone method of array does?Can anybody explain with an example?
Thanks
Veena
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The clone method works the same for any object. It checks that the class of the object on which was invoked implements Cloneable, otherwise throws CloneNotSupportedException. Then creates another object of the same class as "this" object, and copies the content of its instance fields to the clone object. If the field is a primitive the value of such variable is copied. If the field is of reference type, again the content of such variable is copied. The content of a variable that points to an object is the address of that object, a pointer to it. Thus the both the clone object, anf the cloned one share the same non-primitive fields. Because the objects pointed to by such fields are not copied themselves, but only the pointers to them.
The fields of an array object are its length and elements hold by the array. If these elements are primitives the two objects receive separate values of such elements. However if the elements are objects the clone and cloned objects share them as I explained above.
Read the API for Object.clone() and Cloning Objects by Bruce Eckel if you are interested.
[ April 27, 2003: Message edited by: Jose Botella ]
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you clone say, an array, you get a new array object but both will share the subarrays.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Veena -
Halt ! It's the FOCUS police!
Just so anyone new to the ranch isn't confused, the clone() method is NOT on the SCJP 1.4 exam... yeah!
Proceed
-Bert
 
Veena Pointi
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bert Bates:
Veena -
Halt ! It's the FOCUS police!
Just so anyone new to the ranch isn't confused, the clone() method is NOT on the SCJP 1.4 exam... yeah!
Proceed
-Bert


This question has made me to decide buy your book .Actually I read R&H(Simon roberts & philip heller) book one year ago .But couldn't give SCJP2 then.Now again I have made my mind to give the exam but SCJP1.4.I thought I will just upgrade from whatever info I get on net.But it wasn't easy.Have checked book reveiws.Decided to go with following 2 books
1. "Sun certified programmer & Developer for java 2 study gide" by kathy sierra & Bert Bates
2.Java 2 Programmer exam cram 2 by William brogden & Marcus Green
And will give mock exams after done with reading.
Please anybody suggest me if there are any other resources you think I should go through to do better(above 90% ) in the SCJP1.4 exam....
Thanks
Veena
 
Friends help you move. Good friends help you move bodies. This tiny ad will help:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic