Enum and Cloning

What is enum and cloning for? i seen the urls at java
but still do not understand..
please do give examples thanks.
jedistarAsked:
Who is Participating?
 
BogoJokerCommented:
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
0
 
Manikandan ThiagarajanSenior consultantCommented:
0
 
jedistarAuthor Commented:
nice, how do u use cloning, samples?
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
BogoJokerCommented:
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
0
 
jedistarAuthor Commented:
Do need a sample code to understand this thanks.
0
 
BogoJokerCommented:
Here is some direct code:
http://javaalmanac.com/egs/java.lang/CloneObject.html

Did you want an application?
0
 
jedistarAuthor Commented:
jus some sample codes will do
0
 
jedistarAuthor Commented:
ahh don't really understand that url code
0
 
BogoJokerCommented:
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;
        }
    }

Example:
// 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
0
 
jedistarAuthor Commented:
is this shallow or deep cloning
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.