1. Home
  2. Computing & Technology
  3. Java

Association

By , About.com Guide

Definition:

The association relationship is a way of describing that a class knows about and holds a reference to another class. This can be described as a "has-a" relationship because the typical implementation in Java is through the use of an instance field. The relationship can be bi-directional with each class holding a reference to the other. Aggregation and composition are types of association relationships.

Examples:

Imagine a simple war game where there is a AntiAircraftGun class and a Bomber class. Both classes need to be aware of each other as they are designed to destroy each other:

public class AntiAirCraftGun {

  private Bomber target;
  private int positionX;
  private int positionY;
  private int damage;

  public void setTarget(Bomber newTarget)
  {
    this.target = newTarget;
  }

  //rest of AntiAircraftGun class
}

public class Bomber {

  private AntiAirCraftGun target;
  private int positionX;
  private int positionY;
  private int damage;

  public void setTarget(AntiAirCraftGun newTarget)
  {
    this.target = newTarget;
  }

  //rest of Bomber class
}

The AntiAirCraftGun class has-a Bomber object and the Bomber class has-a AntiAirCraftGun object.

Glossary:

# A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Explore Java
About.com Special Features

The Best Web Trends of the Decade

A look back at the best innovations, ideas and technologies over the last 10 years, More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Java
  4. Java Glossary
  5. A
  6. Definition for the Term: Association>

©2009 About.com, a part of The New York Times Company.

All rights reserved.