class
is like a structure, but can contain methods
(functions).
main
method which must be declared as
public static void main(String [] args) { body }
application
) is executed by invoking the interpreter
on a class containing a main
method. Execution begins at that method.
instance variables
.
C
.
val
of a class Counter
, and a
variable ctr
of that class type, we would write ctr.val
.
methods
invoked
).
up
of a class Counter
, and a
variable ctr
of that class type, we would write ctr.up()
.
state
of objects
of that class and the methods defining the class' behavior
.Counter
class containing
val
containing the value of the counter
up
, that increments the counter's value by 1
down
, that decrements the counter's value by 1
class Counter { void up() {...} void down() {...} int val; }
Stack
class containing
arr
, that will contain the elements of the stack
top
, that will contain the index of the top of the stack
push
that pushes a value on the stack
pop
that removes the top element of the stack
isEmpty
that returns whether the stack has any elements
class Stack { void push(??? val) {...} ??? pop() {...} boolean isEmpty() {...} int top; ??? arr[]; }
Window
class containing
width
, that will contain the width (in pixels) of the window
height
, that will contain the height (in pixels) of the window
x
, that will contain the horizontal position (in pixels) of the window
y
, that will contain the vertical position (in pixels) of the window
fgcolor
, that will contain the text color of the window (where Color is itself a
class)
bgcolor
, that will contain the background color of the window (where Color is itself a
class)
text
, that will contain the text currently in the window
move
that changes the position of the window
resize
that changes the size of the window
setFgColor
that changes the text color of the window
getFgColor
that returns the text color of the window
setBgColor
that changes the background color of the window
getBgColor
that returns the background color of the window
insert
that will insert text into the window
class Window { void move(int newX, int newY) {...} void resize(int new width, int newHeight) {...} void setFgColor(Color newFgColor) {...} Color getFgColor() {...} void setBgColor(Color newBgColor) {...} Color getBgColor() {...} void insert(String newText, int where) {...} int width, height; int x, y; Color fgColor, bgColor; String text; }
Counter c1; Stack s2; Window w3;
Objects
(Instances
)
instance
of the class.
refer
to them.
reference
to objects of the class.
reference variables
new
operator
new Counter()
new Stack()
new Window()
reference
to the newly created
object.
Counter counter = new Counter()
Stack stack = new Stack()
Window window = new Window()
Constructors
- Initializing Objects
new
creates the object it invokes a method, known as a
constructor
to initialize the object
new
-- the programmer
does not call it directly.
Counter() {val = 0;}
Default Constructors
and Constructors with Arguments
default constructors
Counter(int val) {...}
// pass in initial value
Stack(int capacity) {...}
// pass in size of stack
Window(int width, int height) {...}
// pass in initial height and width
this
Counter(int val) {...}
?
Counter(int val) {val = val;} // ??? both val's refer to the parameter
this
refers to the current object
Counter(int val) {this.val = val;} // this.val -> val instance variable of the current object
OperatingSystem
which contains a method:
registerForShutdownNotification(Window window) {...}i.e., calling this method registers the passed window so that it is notified when the system is about to shut down.
Window
is created, we need the object to pass itself to the above method--
this is achieved using this
:
Window(...) { // Window constructor ... operatingSystem.registerForShutdownNotification(this); }
Overloading
Counter()
and Counter(int val)
)
public class Counter { public Counter() {val = 0;} public void up() {val++;} public void down() {val--;} public int get() {return val;} public String toString() {return val+"";} private int val; }
Notes:
this
here-- no clash with local vars or parms
toString
allows us to obtain a String representation of the object (more convenient than
a print method)
main
Method
A main
method is often added to a class whose execution will either 'show-off'
the behavior of the class, or test the class (basically the same thing).
public class Counter { public Counter() {val = 0;} public void up() {val++;} public void down() {val--;} public int get() {return val;} public String toString() {return val+"";} // The String concat, '+', converts integers to String automatically private int val; public static void main(String [] args) { Counter c = new Counter(); System.out.println("After initialization: " + c.get()); System.out.println("After initialization, using toString():" + c.toString()); // The String concat, '+', calls an object's toString automatically System.out.println("After initialization, using toString() implicitly: " + c); System.out.println("going up!"); for (int i = 0; i < 10; i++) { c.up(); System.out.println("The counter after " + i + " up(s) is now: " + c); } System.out.println("down we go!"); for (int i = 0; i < 10; i++) { c.down(); System.out.println("The counter after " + i + " down(s) is now: " + c); } } }