SE450: Basics: Fields [30/63] Previous pageContentsNext page

Fields can be used to distinguish different objects of a class.

file:Main.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
package basics.fields;
public class Main {
  private Main() {}
  static public void main (String[] args) {
    //stdlib.Trace.graphvizShowSteps (true); stdlib.Trace.run ();
    Int x = new Int(42);
    Int y = new Int(27);
    System.out.println(x);
    System.out.println(y);
  }
}
final class Int {
  private final int v;
  public Int(int v) { this.v = v; }
  public String toString() { return "Int(" + v + ")"; }
}

The object diagram at line 00008 is:

+--------------+   +--------------+      +---------------------+
|   i0 : Int   |   |   i1 : Int   |      |  0 : Main.main ARI  |
|   --------   |   |   --------   |      |  -----------------  |
+--------------+   +--------------+      +---------------------+
|   v:int = 42 |   |   v:int = 27 |      |  x:Int = i0         |
+--------------+   +--------------+      |  y:Int = i1         |
                                         +---------------------+

It is traditional in the UML to show objects, but not ARIs.

This can be confusing.

Although the names given to objects are arbitrary, perhaps the following are more helpful:

+--------------+   +--------------+
|    x : Int   |   |    y : Int   |
|    -------   |   |    -------   |
+--------------+   +--------------+
|   v:int = 42 |   |   v:int = 27 |
+--------------+   +--------------+

Unfortunately, this convention is not always more helpful.

trace-basics-fields-003-Int_14 trace-basics-fields-004-Main_main_6 trace-basics-fields-005-Main_main_7 trace-basics-fields-006-Int_14 trace-basics-fields-007-Main_main_7 trace-basics-fields-008-Main_main_8 trace-basics-fields-009-Int_toString_15 trace-basics-fields-010-Main_main_9 trace-basics-fields-011-Int_toString_15 trace-basics-fields-012-Main_main_10

Previous pageContentsNext page