

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


with Java, for Beginners
“Has a” RelationshipNull, NullPointerExceptionCall by reference
Either “null” or a “heap address” null
means currently not pointing at any location
new Counter()
allocates space for the actual Counter
on the heap, and initializes it c
actually allocates space to hold a
reference
to a
Counter
¾^
Method does not return (if declared in a method) ¾^
Dr. J Interactions pane is not reset (if declared in Interactionspane)
The assignment makes
c
refer
to
the new Counter
An object of type A has an instance variable which is anobject whose type is B. (A “has a” B)
E.g: A Freshman object whose room is of reference typeDormRoom
The UML diagrams
show instance variables and methods
of Freshman and DormRoom object:^
UML(Universal Modeling Language) industry standard used todescribe classes in OOP
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){
num = n;bldgName = b; } public String getLocation(){
return num + " " + bldgName; } }
DormRoom room = new DormRoom(208, "Hill");> room.getLocation()"208 Hill"
public class Freshman{
private String name;private DormRoom room;public Freshman(String n, DormRoom r){
name = n;room = r; } public String getName(){ return name;}public DormRoom getRoom(){ return room;} }
DormRoom room = new DormRoom(208, "Hill");> Freshman f = new Freshman("jo", room);> f.getName()"jo"> f.getRoom().getLocation()"208 Hill"