Learn how to a build a cloud-first strategyRegister Now


Enum and Cloning

Posted on 2006-04-08
Medium Priority
Last Modified: 2006-11-18
What is enum and cloning for? i seen the urls at java
but still do not understand..
please do give examples thanks.
Question by:jedistar
  • 5
  • 4
LVL 17

Accepted Solution

BogoJoker earned 600 total points
ID: 16409927
Hi jedistar,

clones are deep copies, completely new object but exactly like the first.  There is something your probably more familer with, a shallow copy, here are the differences.

Shallow Copy: (Just a reference)
Integer i = new Integer(10);
Integer j = i;
// Now any change to i also changes j (as long as i does not make a new reference)

Deep Copy (simulated, this is pretty much what clone does):
Integer i = new Integer(10);
Integer j = new Integer(i.intValue());
// Now any change to i has no affect on j

I am not too certain on enums.  They are just objects, inside an object. =)
Joe P

Assisted Solution

by:Manikandan Thiagarajan
Manikandan Thiagarajan earned 400 total points
ID: 16409985

Author Comment

ID: 16410044
nice, how do u use cloning, samples?
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 17

Expert Comment

ID: 16410049
If you follow stmani's link it had a nice suggestion.
One was to make a clone of an object as a backup.
Do stuff to the original object to see what happens (maybe compare the clone and original now), then when you want to "rollback" you would just go back to the clone that you made.  Or similarly just make all changes to the clone and compare it to the unchanged original.

Joe P

Author Comment

ID: 16410111
Do need a sample code to understand this thanks.
LVL 17

Expert Comment

ID: 16410185
Here is some direct code:

Did you want an application?

Author Comment

ID: 16410196
jus some sample codes will do

Author Comment

ID: 16410198
ahh don't really understand that url code
LVL 17

Expert Comment

ID: 16410214
I'll try to add some comments.

// Notice that this implements clonable, therefore it should write the
// clone() method, it must return an object.
    class MyClass implements Cloneable {
        // Just a simple constructor
        public MyClass() {

        //  Once again the goal of a clone() method
        // is to make a COPY of the current object.  So
        // If there were class variables you set all of them
        // for the new variable (in this case theClone)
        public Object clone() {
            Cloneable theClone = new MyClass();
            // Initialize theClone.
            return theClone;

// Here's one object myObject, we create it  
MyClass myObject = new MyClass();
// This new object myObjectClone is a clone of myObject
// it will be exactly the same as the above, but any change
// to one will NOT affect the other
MyClass myObjectClone = (MyClass)myObject.clone();

Arrays are automatically cloneable:
// This is a practical example, arrays can always be cloned
int[] ints = new int[]{123, 234};
int[] intsClone = (int[])ints.clone();

Joe P

Author Comment

ID: 16414580
is this shallow or deep cloning

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month20 days, 17 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question