SE 450 Fall 2001/2002

Week 2 Lecture Notes


Object Initialization


Constructors


Initialization Blocks


Calling methods


Variable, Object, Class, Type

Variables have types, objects have classes.

Object Reference: this

You can use this inside a method

Static Fields

Static field: one per class, rather than one per object. class IDCard { public long id; protected static long nextID = 0; public IDCard() { id = nextID++; } }

Static Methods

A static method class IDCard { public long id; protected static long nextID = 0; ... public static void skipID() { nextID++; } }

Final Variables


Arrays

for (int y = 0; y < mat.length; y++) { for (int x = 0; x < mat[y].length; x++) mat[x][y] = 0.0; }

Wrapper classes


Strings


toString Method of Objects

The toString method converts objects to strings. public class Point { public int x, y; ... String toString() { return "(" + x + "," + y + ")"; } } Then, you can do Point p = new Point(10,20); System.out.println("A point at " + p); // Output: A point at (10,20)

Exceptions


Applets and their Methods


Applet examples