• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Can a object contain another object?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 214
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Memory for objects is always allocated on the heap.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic