
1
Introduction to Programming
w
ava,
or
eg
nner
“
”
Null, NullPointerException
Call by Reference
References Recap
The value of reference variable is
Either “null” or a “heap address”
null means currently not pointing at any location
Counter c = new Counter();
new Counter() allocates space for the actual Counter
on the heap, and initializes it
cactually allocates space to hold a reference to a
•c is placed on the stack and existent as long as
¾Method does not return (if declared in a method)
¾Dr. J Interactions pane is not reset (if declared in Interactions
pane)
The assignment makes crefer to the new Counter
ESE112 2
“Has a” Relationship or Composition
An object of type A has an instance variable which is an
object whose type is B. (A “has a” B)
E.g: A Freshman object whose room is of reference type
The UML diagrams show instance variables and methods
of Freshman and DormRoom object:
UML(Universal Modeling Language) industry standard used to
describe classes in OOP
ESE112 3
DormRoom Code
> DormRoom room = new DormRoom(208, "Hill");
> room.getLocation()
208 Hill"
public class DormRoom{
private int num;
private String bldgName;
public DormRoom(int n, String b){
=
bldgName = b;
}
public String getLocation(){
return num + " " + bldgName;
}
}
ESE112 4