GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Can a object contain another object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can a object contain another object?" Watch "Can a object contain another object?" New topic
Author

Can a object contain another object?

Debbie Tom
Greenhorn

Joined: Sep 12, 2005
Posts: 11
In java, aggregation of objects is only through reference.(Read it in Certification book by Khalid Mughal)
But..

[ September 13, 2005: Message edited by: Michael Ernest ]
Tom Billings
Greenhorn

Joined: Sep 12, 2005
Posts: 16
Hi Debbie!

No, a1 contains a reference to the newly created object which in turn holds a reference to the array object. Got it?
Debbie Tom
Greenhorn

Joined: Sep 12, 2005
Posts: 11
Hi Tom,

Thanks for the reply.Does this statement hold good ?
objects cannot contain objects.

if so, the example does contain a object within object.
Does the memory for the array arr is allocated within the memory of a1?

Wat i understand from ur reply is the object a1 only contains a reference to object arr.Can u explain a bit more on this?

Thanks!
Tom Billings
Greenhorn

Joined: Sep 12, 2005
Posts: 16
Hi Debbie!

Yes, the statement "Objects cannot contain objects" holds good. Arrays, even of primitive types, are considered objects i Java.

When instanciating an object of class ArrayOb an array-object is also instanciated and assigned to the instance variable arr.

So, a1 holds a reference to the newly created ArrayOb-object, that's what your assignment does, right?
During instantiation of ArrayOb an int[]-object gets created and assigned to the instance variable arr, right?

This concludes that a1 holds a reference to an ArrayOb-object which in turn holds a reference to an int[]-object.

"int[]-object" isn't actually the proper way of putting it but a bit more descriptive in this case. An array implicitly extends java.lang.Object so an array is an instance of Object, to be correct.
Debbie Tom
Greenhorn

Joined: Sep 12, 2005
Posts: 11
Hi Tom,

My understanding is a object cannot contain another object.
Even though the object a1 appears to contain array(object) arr it contains only a reference to it.The memory for arr is allocated outside of a1.

Each object of the class arrayOb will contain a separate memory of array arr. :

Am i right?
Tom Billings
Greenhorn

Joined: Sep 12, 2005
Posts: 16
Yes, conceptually you're right. How the data is physically stored I'm not really sure of and I actually don�t care
Debbie Tom
Greenhorn

Joined: Sep 12, 2005
Posts: 11
Thanks Tom!

It appears as though a object contains another object but still it does not.It contains only reference.

Can some one shed light upon how the memory will be allocated?
May be tat can make things clear...
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
I think you have it but as a practical matter you would be better served to think of objects as containing other objects. Let's say the compiler placed objects directly inside other objects. How could you discern that? They are functionally equivalent.
Edwin Keeton
Ranch Hand

Joined: Jul 10, 2002
Posts: 214

Memory for objects is always allocated on the heap.


SCJP, SCWCD
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
My two cents:

We always prefer to teach that objects all stand alone in the heap, but that they might have reference variables that provide a way of hooking them together. There are several reasons why this might be a better visualization:

1 - It allows for the case where two or more objects 'have' or 'refer to' another single object. In this case, if two objects refer to the same object, in which of the two referers would you say the refer-ee lives?

2 - It makes thinking about garbage collection easier. If you use the analogy that an object contains another object, then what happens when you null the referers reference? Does the refer-ee object get 'kicked out'? (if so, where does it get kicked out to?) Also, this ties back to the first point, if you've just 'kicked out' an object, what if another object still has a reference? How does this effect the object's eligibility for the garbage collector?

3 - What happens when you lose your reference to the referring object? Does that mean you've lost the refer-ee object? Not if another object also referred to the refer-ee object.

So, once again, we recommend that you visualize objects as standing alone in the heap, with lots of links to each other.

Oh, here's another thing, when you start thinking about inheritance, we often find that it might be useful to imagine that when you make, say a Dog object, there's a little, inherited Object object inside the Dog object... so we'd really hate to overload our visual metaphors

hth,

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
 
GeeCON Prague 2014
 
subject: Can a object contain another object?