how should i prevent the shallow copying?

every class i think i would need to copy i need to do the 'clone' function?
is there a 'copy constructor'?

the thing is -
i have a constructor that gets objects as parameters. how do i copy it into the data member?

i started to code now, after a long break, and i don't know how, but i forgotted lot of basic stuff.. :(
and lot of other things comes from c++ (i coded before)..

Recommended Answers

All 7 Replies

You can always implement copy constructors in your class:

public Class yourClass
{
   public yourClass(yourClass yourClassObject)
   {
      //implement
   }
}

Other than that, you need to read the API about mutable and immutable objects to help you decide how to implement their copy in the constructor.

and if i do:

MyClass a,b;
// ... there is now something in b
a=b;

what would happen?

Objects are always by reference in Java, which means that a=b will point a to the place in the heap the b resides - any future changes to b will affect a as well.

Objects are always by reference in Java, which means that a=b will point a to the place in the heap the b resides - any future changes to b will affect a as well.

does it means that if a is local b would hold garbage?

You were talking about objects - in Java objects are always on the heap, never local on the stack, so the scenario you have mentioned cannot happen.

wow, i'm confused..
thx on all the help!

Well, it was not my intention to confuse you - what are you confused about? I'll try to explain it more thoroughly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.