0

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)..

2
Contributors
7
Replies
8
Views
6 Years
Discussion Span
Last Post by apines
1

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.

0

and if i do:

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

what would happen?

Edited by anilopo: n/a

1

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.

Edited by apines: n/a

0

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?

1

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.

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.