Connecting Tech Pros Worldwide Forums | Help | Site Map

Object Cloning and Object Creation using new

dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#1: May 27 '09
Have a look at my code.

Expand|Select|Wrap|Line Numbers
  1. public class MyClass{
  2.  private int a=0;
  3.  private int b=0;
  4.  
  5.  MyClass(int a,int b){
  6.     this.a = a;
  7.     this.b = b;
  8.  }
  9.  
  10.  public void setA(int a){
  11.     this.a = a;
  12.  }
  13.  
  14.  public void setB(int b){
  15.     this.b = b;
  16.  }
  17.  
  18.  public int getA(){return a;}
  19.  public int getB(){return b;}
  20. }
  21.  
Here my main program goes ...
Expand|Select|Wrap|Line Numbers
  1. public static void main(String a[]){
  2.  MyClass obj = new MyClass(100,100);
  3.  MyClass clone_obj = (MyClass)obj.clone();
  4. }
  5.  
Now my question is the two lines here doing the same job.
Which one needs more headache?

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: May 27 '09

re: Object Cloning and Object Creation using new


Quote:

Originally Posted by dmjpro View Post

Expand|Select|Wrap|Line Numbers
  1. public class MyClass{
  2.  private int a=0;
  3.  private int b=0;
  4.  
  5.  MyClass(int a,int b){
  6.     this.a = a;
  7.     this.b = b;
  8.  }
  9.  
  10.  public void setA(int a){
  11.     this.a = a;
  12.  }
  13.  
  14.  public void setB(int b){
  15.     this.b = b;
  16.  }
  17.  
  18.  public int getA(){return a;}
  19.  public int getB(){return b;}
  20. }
  21.  
Here my main program goes ...
Expand|Select|Wrap|Line Numbers
  1. public static void main(String a[]){
  2.  MyClass obj = new MyClass(100,100);
  3.  MyClass clone_obj = (MyClass)obj.clone();
  4. }
  5.  
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
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#3: May 27 '09

re: Object Cloning and Object Creation using new


Quote:

Originally Posted by JosAH View Post

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. ;)
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#4: May 27 '09

re: Object Cloning and Object Creation using new


Quote:

Originally Posted by dmjpro View Post

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.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#5: May 27 '09

re: Object Cloning and Object Creation using new


Quote:

Originally Posted by dmjpro View Post

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
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#6: May 28 '09

re: Object Cloning and Object Creation using new


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

Expand|Select|Wrap|Line Numbers
  1. public class MyClass implements Cloneable/*The previous error i fixed it here ;)*/ {
  2.   public MyClass(...){} //plain constructor
  3.   public MyClass(MyClass obj){ //copy constructor
  4.    this(...);
  5.   }
  6.   public Object clone() throws CloneNotSupportedException{
  7.    return super.clone(); //this implementation is required because clone is protected in Object class
  8.   }
  9.  //some code goes here
  10. }
  11.  
Now my creation of objects goes here.
Expand|Select|Wrap|Line Numbers
  1. MyClass obj = new MyClass(...);
  2.  
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.

Expand|Select|Wrap|Line Numbers
  1. MylCass clone_obj = (MyClass)obj.clone();
  2.  
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.
Expand|Select|Wrap|Line Numbers
  1. MyClass second_obj = new MyClass(...);
  2.  
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?

Expand|Select|Wrap|Line Numbers
  1. MyClass source_obj = new MyClass(...);
  2. MyClass copy_obj = new MyClass(source_obj);
  3.  
Here it also needs a source object.Memory allocation and calling a constructor(copy), 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 ;)
Reply