• Post Reply Bookmark Topic Watch Topic
  • New Topic

Aggregation in Java  RSS feed

 
omkar patkar
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I came across this point under the topic of "Aggregation" in Khalid Mughal's book. It says :-

"Java supports aggregation of objects by references, since objects cannot contain other objects explicitly. The fields can only contain values of primitive data types or references to other objects."

i did not get this point. So, i tried following example of my own :-

==========================
package classesBasic;

class A
{
int i;

A(int value)
{
this.i = value;
}
}

class B
{
int j;
A variableOfClassA = new A(2);

B(int value)
{
this.j = value;
}

B()
{

}
}
public class ClassesBasicPart1 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

A var1 = new A(3);

B var2 = new B(var1.i);
B var3 = new B();

System.out.println("The value of A is "+var1.i);
System.out.println("The first value of B is "+var2.j);
System.out.println("The value of instance variable in B is "+var2.variableOfClassA);
System.out.println("The value of instance variable of instance variable in B is "+var2.variableOfClassA.i);
}

}
==========================

and the program compiles successfully and gives following output :-
OUTPUT:-
The value of A is 3
The first value of B is 3
The value of instance variable in B is classesBasic.A@923e30
The value of instance variable of instance variable in B is 2


When the prg compiles successfully then what does the above point convey ?

Please guide me !
Thanks
Omkar-'A'
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a subtle point, but basically it has to do with how objects are allocated on the heap.

Each object has an exclusive location in memory. So even if X has-a Y, then Object Y is not really living inside the memory allocated to Object X. Instead, these objects have exclusive (non-overlapping) addresses, and Object X only contains a reference to Object Y.
 
omkar patkar
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Marc !

From the line i had quoted, i thought that i can only declare object references as shown in bold :-

class A
{
int i;

....
....
}

class B
{
int j;
A variableOfClassA ; // and not as shown below

A variableOfClassA = new A(2);

...
....
...
...

}

According to my interpretation, i thought that we cannot create objects (using "new")

But can you please modify my example to explain that point more clearly...i mean i want to know when the prg will fail ?

Thanks
Zomkar
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a "behind the scenes" detail that should be (mostly) invisible to you when writing code. It's not something that you can demonstrate by causing a program to fail.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by omkar patkar:
But can you please modify my example to explain that point more clearly...i mean i want to know when the prg will fail ?


In C++ (if I remember correctly), the line

A variableOfClassA = new A(2);

would look like

A* variableOfClassA = new A(2);

The * behind the type name tells the compiler that we want to have a pointer (kind of a reference) to an object of type A. In Java, we don't need that syntax, because there is no other way to "aggregate" objects.

In C++, on the other hand, you don't need to use a pointer. Instead you could write

A variableOfClassA(2);

(or something like that - it's been a long time since I wrote my last line of C++). This means that the object is directly embedded into the the containing object.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!