Java Reference
In-Depth Information
For example, the
java.awt.Button
class declares the following methods:
public void addActionListener(ActionListener a)
public void removeActionListener(ActionListener a)
According to the JavaBeans method naming convention, the
Button
class is therefore
a source of events of type
ActionEvent
, and another object can register and unregister
itself with the
Button
to listen to the
ActionEvent
by calling the appropriate add and
remove method.
The exam objectives do not specifi cally state that you need to know the event listener
methods, so you may or may not see this topic on the exam. However, it is useful
information that is worth knowing as a Java developer because JavaBeans show up in all
sorts of Java technologies.
Instance Methods
An
instance method
is a nonstatic method of a class. They are referred to as instance
methods because they represent the behaviors of each instance of the class. Instance
methods are also referred to as member methods, member functions, or simply methods.
An instance method can only be invoked on an instance of the class. Without an
instance of the class, the method does not exist and it does not make sense to attempt to
invoke it. You can't drive a car until you manufacture the car. You can't cook in the kitchen
until you build the house. Methods are behaviors of the objects, so the objects need to exist
before they can perform their desired behaviors.
You use the dot operator on a reference to invoke an instance method. Let's look at an
example. The following
Customer
class contains one constructor and fi ve instance methods:
1. public class Customer {
2. private String name;
3. private int id;
4.
5. public Customer(int id, String name) {
6. setId(id);
7. this.setName(name);
8. }
9.
10. public void setName(String name) {
11. this.name = name;
12. }
13.
Search WWH ::
Custom Search