Object Cloning and Object Creation using new

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    Object Cloning and Object Creation using new

    Have a look at my code.

    Code:
    public class MyClass{
     private int a=0;
     private int b=0;
    
     MyClass(int a,int b){
    	this.a = a;
    	this.b = b;
     }
    
     public void setA(int a){
    	this.a = a;
     }
    
     public void setB(int b){
    	this.b = b;
     }
    
     public int getA(){return a;}
     public int getB(){return b;}
    }
    Here my main program goes ...
    Code:
    public static void main(String a[]){
     MyClass obj = new MyClass(100,100);
     MyClass clone_obj = (MyClass)obj.clone();
    }
    Now my question is the two lines here doing the same job.
    Which one needs more headache?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by dmjpro
    Code:
    public class MyClass{
     private int a=0;
     private int b=0;
    
     MyClass(int a,int b){
    	this.a = a;
    	this.b = b;
     }
    
     public void setA(int a){
    	this.a = a;
     }
    
     public void setB(int b){
    	this.b = b;
     }
    
     public int getA(){return a;}
     public int getB(){return b;}
    }
    Here my main program goes ...
    Code:
    public static void main(String a[]){
     MyClass obj = new MyClass(100,100);
     MyClass clone_obj = (MyClass)obj.clone();
    }
    Now my question is the two lines here doing the same job.
    Which one needs more headache?
    That code snippet won't work because MyClass doesn't implement the Cloneable interface. Better test your code first before you post an old riddle.

    kind regards,

    Jos

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Originally posted by JosAH
      That code snippet won't work because MyClass doesn't implement the Cloneable interface. Better test your code first before you post an old riddle.

      kind regards,

      Jos
      But ... I simply wrote it and posted it. If i ran this code i would get the error and correct it, but my question never changed.

      Well the code may confuse the novice. Thanks for correcting it. But you should have answered to my question. ;)

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by dmjpro
        But ... I simply wrote it and posted it. If i ran this code i would get the error and correct it, but my question never changed.

        Well the code may confuse the novice. Thanks for correcting it. But you should have answered to my question. ;)
        Nah, we are not your code service. Make sure your code compiles before posting it.
        Do you mean that you posted it so that you can confuse novices? Don't do that. The forum is for helping people not confusing them.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by dmjpro
          Now my question is the two lines here doing the same job.
          No they don't when you think about it the constructor creates an object out of the blue sky, out of nothing while the cloning process needs a 'source' object to build the clone from. In SmallTalk it's called an 'exemplar' and Java's clone() method is a poor immitation thereof.

          Better use a 'copy constructor' if you know the class, i.e. a constructor with a single argument of the same type as the class where the constructor is located.

          And I agree with r035198x: it is silly to confuse beginners with this crap because the example itself contained errors and mentioning 'headache' isn't a wise remark either. It only could've raised confusion and didn't explain anything.

          kind regards,

          Jos

          Comment

          • dmjpro
            Top Contributor
            • Jan 2007
            • 2476

            #6
            Well ..good explanation.
            Now ...have some interesting issues ;)

            Code:
            public class MyClass implements Cloneable/*The previous error i fixed it here ;)*/ {
              public MyClass(...){} //plain constructor
              public MyClass(MyClass obj){ //copy constructor
               this(...);
              }
              public Object clone() throws CloneNotSupportedException{
               return super.clone(); //this implementation is required because clone is protected in [B]Object[/B] class
              }
             //some code goes here
            }
            Now my creation of objects goes here.
            Code:
            MyClass obj = new MyClass(...);
            Here what happens, if the class is in first time use then class gets loaded(the class loading ... needs lot of work) then allocates the memory for the new object and then calls the plain constructor.

            Code:
            MylCass clone_obj = (MyClass)obj.clone();
            Here, the source object(here obj) required(class loading removed here). First it allocates the memory and then simply copies the states of object.Cloning an object, it does not call the constructor. Is it right?

            Again i am creating an object using plain construtor.
            Code:
            MyClass second_obj = new MyClass(...);
            Here class loading removed. It allocates memory and then calls the plain constructor. Cloning of an object and creating an object using plain constructor, the difference is no source object required and one more thing what i think that in case of cloning Native call required. Is it a more headache than simply calling the constructor?

            Code:
            MyClass source_obj = new MyClass(...);
            MyClass copy_obj = new MyClass(source_obj);
            Here it also needs a source object.Memory allocation and calling a constructor(cop y), it's mostly same as cloning an object as well as creating a new object(using plain constructor) after the class gets loaded.
            But you told that it's better to use copy constructor than cloning an object.
            The reason is because of native call or something else?

            If any wrong with my explanation then pleas correct it ;)

            Comment

            Working...