The Java Program: PassByRef.java
1 // Java objects are references; from Arnold and Gosling
2
3 class CelestialBody {
4 long idNum;
5 String name;
6 CelestialBody orbits;
7
8 static long nextID = 0;
9
10 CelestialBody () {
11 idNum = nextID++;
12 }
13
14 CelestialBody (String n, CelestialBody o) {
15 this();
16 name = n;
17 orbits = o;
18 }
19
20 public String toString () {
21 String desc = idNum + " (" + name + ")";
22 if (orbits != null) desc += " orbits " + orbits.toString();
23 return desc;
24 }
25 }
26
27 class PassByRef {
28 public static void main (String[] args) {
29 CelestialBody sirius = new CelestialBody ("Sirius", null);
30
31 System.out.println ("before: " + sirius);
32 commonName (sirius);
33 System.out.println ("after: " + sirius);
34 }
35
36 public static void commonName (CelestialBody bodyRef) {
37 bodyRef.name = "Dog Star";
38 bodyRef = null;
39 }
40 }