473,396 Members | 2,108 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Object Cloning and Object Creation using new

dmjpro
2,476 2GB
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?
May 27 '09 #1
5 2164
JosAH
11,448 Expert 8TB
@dmjpro
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
May 27 '09 #2
dmjpro
2,476 2GB
@JosAH
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. ;)
May 27 '09 #3
r035198x
13,262 8TB
@dmjpro
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.
May 27 '09 #4
JosAH
11,448 Expert 8TB
@dmjpro
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
May 27 '09 #5
dmjpro
2,476 2GB
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 ;)
May 28 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Vincent | last post by:
Hey, I have a problem to understand the underlaying of clone method. Hope someone can help me out. Let's say, clonedObject = originalObject.clone() (syntax may be wrong, but you know what I...
2
by: Ryan Mitchley | last post by:
Hi all I have code for an object factory, heavily based on an article by Jim Hyslop (although I've made minor modifications). The factory was working fine using g++, but since switching to the...
13
by: ahaupt | last post by:
Hi all, I'm implementing the Clone() method through the ICloneable interface and don't quite know how deep I need to go for a deep copy. Example: class A: ICloneable { object _val;
6
by: solex | last post by:
Hello, I am trying to use serialization to copy objects. The object in question "Institution" inherits from a parent object "Party" both are marked as <Serializable()>. Initially I can copy an...
8
by: Raterus | last post by:
Hi, I don't even know if this can be done.. Lets say I have two objects, --- Public Class Employee End Class
4
by: Mike | last post by:
I have a class "Call". I have instantiated a Call object in one class then passed a pointer to that object to another class. How do I create a *copy* of that Call object using only the pointer?
8
by: Noozer | last post by:
I'm looking for a way to generate a "clone" of an object. Right now I need to write a Clone function for every class that make and I'd like to have a generic routine. Instead of doing this: For...
13
by: JohnQ | last post by:
Why would anyone write: class SomeThing // class littered with non-domain single-instancing code :( { private: SomeThing(); static SomeThing* pInstance_; public: static SomeThing*...
15
by: mark.norgate | last post by:
Hello I want to create a reference to an object, so that changes to the referenced object are reflected in the other object. Like this: object o = 123; object p = o; o = 456;
9
by: Peter Webb | last post by:
I want to animate one object moving in front of another. I cannot re-render the background as the object moves, as it would be extremely time consuming. This is what I would like to do. I draw...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.