Each object has its own memory made up of other objects. Is this still valid in Java?
Put another way, you create a new kind of object by making a package containing existing objects. Thus, you can build complexity into a program while hiding it behind the simplicity of objects.
from Thinking in Java
Are not all objects independent of each other in Java? Is it possible to create a super object from different objects?
EDIT:
Alan Kay summarized five basic characteristics of Smalltalk way before Java was around. This is one of the characteris开发者_开发百科tics of an OOP according to him. I was wondering if it is still valid for java.
I think you have to distinguish two separate concepts:
In memory each object in Java is a separate entity with its own set of memory (and I believe Smalltalk worked the same way). It can reference other objects, which are held in their own memory, but not "contain" other objects within its memory.
Conceptually, there are objects that "belong" to other objects in some way. For example the char[]
holding the data of a String
is technically a separate object, but the only other object referencing it is the String
(and possibly other String
instances, but let's ignore that at the moment). In this sense, the String
contains the char[]
.
An object can't contain other objects directly in Java - but it can certainly hold references to other objects. I suspect that's what the author is trying to get across.
So if a Customer
object knows about the address, phone number, list of orders, name etc, those would all be separate objects... but instead of having to manage all of those explicitly, the user of a Customer
object just keeps a reference to the object, and accesses the rest of the data via that single object.
精彩评论