I am studying for the Java 8 Programmer II certification, and I'm going over the relationships classes can have. I understand composition, but I wondered if there is relationship considered between the elements of a collection that is a class variable of a class and that class. For instance, consider this code:
public class A {
static ArrayList<String> strings = new ArrayList<String>();
public A() {
strings.add("foo");
}
}
Do we say that the String elements of strings have a relationship with class A?
First of all we should make clear if classes A
and String
would be in relationship if class A
would have simple String
field like so:
public class A {
static String aString;
public A() {
aString = "foo";
}
}
... I think so! And that would be "has-a" realtionship - A
has-a String
.
In your example there's a collection of String
s. We can also say that class A
has-a (lot of ;) String
s... You can imagine that class A
has a lot of relationships with String
objects; class A
aggregates them in collection. This is analogous to well known composition example "car 'has' wheels".
You can also say that this is unidirectional one-to-many relationship (unidirectional because only one side of this relationship knows about the other).