Members of a class:
Attributes or Fields (instance variables, data)
For each instance of the class (object), values of attributes can vary, hence they are instance variables
Methods (instance methods)
Person class
Attributes: name, address, phone number
Methods: change address, change phone number
Alice object
Name is Alice, address is ...
Objects must be initialized before use.
We must specify what is the initial state of the object before we can use it.
"new" creates a new object from
specified type:
new String();
new Book();
When a parameter is passed, a copy of the value is made and assigned to the formal parameter:
The getHours() and secondElapsed() methods are instance methods, which means they act on a particular instance of the class
They cannot be invoked “out of the blue”. They must act on a particular object
An instance method is executed in the context of the object it acts upon.
Variables may be declared in:
Class – state variables
Method/constructor – local variables (and parameters)
Inner block of a method – also local variables
When appearing inside an instance method, the “this” keyword denotes a reference to the object that the method is acting upon.
The following are equivalent:
It is usually a bad practice to use the same name for a state variable and a local variable or parameter.
Exception: parameters that correspond to fields
Chapter 7: Objects and Classes
section 7.1, 7.2, 7.3, 7.4, 7.6, 7.9, 7.10, 7.11
Chapter 10: Inheritance
Sections 10.1, 10.2, 10.3, 10.4