SE450: Basics: Fields [30/63] |
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.