• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Aggregation in Java

 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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'
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic