tcTitleRunning
tcTitleSlide
Code
Board
Trace
Description
Here a complex object case.
We have three types of objects:
String, Coor1D, and Point1D.
...
"A"
...
"B"
...
First, we handle String literals.
String strA;
causes two actions:
strA of type String.String.strA = "A";
strA to literal "A"Coor1D c1;
causes two actions:
c1 of type Coor1D.Coor1D.It is exactly the same as what
String strA; does.
c1 = new Coor1D(1);
causes two actions:
creates a new object of type Coor1D.
Coor1D has one field x.
int.This is the constructor of Coor1D:
public class Coor1D {
int x;
public Coor1D(int x) {
this.x = x;
}
...
}
c1 = new Coor1D(1);
causes two actions:
creates a new object of type Coor1D.
Coor1D has one field x.
int.This is the constructor of Coor1D:
public class Coor1D {
int x;
public Coor1D(int x) {
this.x = x;
}
...
}
connects c1 to the new object.
Point1D p1;
as String strA; or Coor1D c1, causes two actions:
p1 of type Point1D.Point1D.p1 = new Point1D(strA, c1);
is the most complex statement of this visualization because of Point1D itself is a complex object.
public class Point1D {
String name;
Coor1D coordinate;
...
}
It has two fields name and coordinate
Since
strA is of type String, andc1 is of type Coor1D,this is a call to constructor
Point1D(String, Coor1D).The first constructor of Point1D is called:
public class Point1D {
String name;
Coor1D coordinate;
public Point1D(String name, Coor1D c) {
this.name = name;
this.coordinate = c;
}
public Point1D(String name, int x) {
...
}
...
}
p1 = new Point1D(strA, c1);
causes a number of actions
Point1D object
public class Point1D {
String name;
Coor1D coordinate;
...
}
p1 = new Point1D(strA, c1);
causes a number of actions
Point1D object strA refers
public class Point1D {
String name;
Coor1D coordinate;
public Point1D(String name, Coor1D c) {
this.name = name;
...
}
...
}
p1 = new Point1D(strA, c1);
causes a number of actions
Point1D object strA refers c1 refers
public class Point1D {
String name;
Coor1D coordinate;
public Point1D(String name, Coor1D c) {
...
this.coordinate = c;
}
...
}
p1 = new Point1D(strA, c1);
causes a number of actions
Point1D object strA refers c1 refers p1 to the new object Point1D p2;
does as expected
p2 of type Point1D.Point1D.It is no different then Point1D p1;, String strA; or Coor1D c1.
p2 = new Point1D("B", 7);
This is the second most complex statement of this visualization.
It is different then
p1 = new Point1D(strA, c1);
"B" is of type String, and7 is of type int,Point1D(String, int).The second constructor of Point1D is called:
public class Point1D {
String name;
Coor1D coordinate;
public Point1D(String name, Coor1D c) {
...
}
public Point1D(String name, int x) {
this.name = name;
this.coordinate = new Coor1D(x);
}
...
}
new
new Coor1D(x)p2 = new Point1D("B", 7);
causes a number of actions
Point1D object
public class Point1D {
String name;
Coor1D coordinate;
...
}
p2 = new Point1D("B", 7);
causes a number of actions
Point1D object public class Point1D {
String name;
Coor1D coordinate;
public Point1D(String name, Coor1D c) {
...
}
public Point1D(String name, int x) {
this.name = name;
...
}
...
}
p2 = new Point1D("B", 7);
causes a number of actions
Point1D object Coor1D object but we have 7. Constructor tells us how to obtain a Coor1D object from 7
public class Point1D {
String name;
Coor1D coordinate;
...
public Point1D(String name, int x) {
this.name = name;
this.coordinate = new Coor1D(x);
}
...
}
new Coor1D(7);
similar to
c1 = new Coor1D(1);
p2 = new Point1D("B", 7);
causes a number of actions
Point1D object Coor1D object. Constructor tells us how to obtain a Coor1D object from 7
public class Point1D {
String name;
Coor1D coordinate;
...
public Point1D(String name, int x) {
this.name = name;
this.coordinate = new Coor1D(x);
}
...
}
new Coor1D(7);
similar to
c1 = new Coor1D(1);
this.coordinate p2 = new Point1D("B", 7);
causes a number of actions
Point1D object Coor1D object. Constructor tells us how to obtain a Coor1D object from 7
public class Point1D {
String name;
Coor1D coordinate;
...
public Point1D(String name, int x) {
this.name = name;
this.coordinate = new Coor1D(x);
}
...
}
new Coor1D(7);
similar to
c1 = new Coor1D(1);
this.coordinate p2 to newly created Point1D object. Objects are not that easy, isn't it?
That's it.
...
String strA;
strA = "A";
Coor1D c1;
c1 = new Coor1D(1);
Point1D p1;
p1 = new Point1D(strA, c1);
Point1D p2;
p2 = new Point1D("B", 7);
...
. 1,3,12 2 3 5 6 6 8 9 9 9 9 9 11 12 12 12 12 12 12 . . . . .
| "A" "B" strA c1 p1 p2
Statements | p1.s p1.c p2.s p2.c
-------------------------------- | --- --- ---- --- -- ---- ---- -- ---- ----
| A B u u u u u u u u
String strA; | A B - u u u u u u u
strA = "A"; | A B A u u u u u u u
Coor1D c1; | A B A - u u u u u u
c1 = new Coor1D(1); | A B A (1) u u u u u u
Point1D p1; | A B A (1) - u u u u u
p1 = new Point1D(strA, c1); | A B A (1) - A (1) u u u
Point1D p2; | A B A (1) - A (1) - u u
p2 = new Point1D("B", 7); | A B A (1) - A (1) - B (7)