The Association Relationship

What is the Association Relationship?

          Association is where you pass a variable from one class to another class. This is done by assigning the passed variable to an instance variable in the targets class. As opposed to composition, association is used when a variable that already exists needs to be used in another class.

Implementing Association in code.

          A good example of implementing this method is by using a button listener. When using button listeners, sometimes you need the button to take action on a certain variable. In the following example, the AssociationExample class is passed to the button, the button assigns the variable to its own instance variable, and then it calls a method when clicked to increment the integer.

AssociationExample.java

ButtonListener.java

UML diagram of the Association Relationship