Sub topic 2
Representing state and behavior with fields and methods
A class describes the objects you can make from it. An object can hold information and do things. In Java, fields hold that information, and methods describe what the object can do.
An object’s state is the information it holds at a given time. A field is a variable declared in a class to hold part of that state. For example, a counter object can have a field named value.
An object’s behavior is what it can do. A method is a named set of instructions. A counter might have a method that adds one to its value.
A counter example
Save this program in a file named Counter.java:
public class Counter {
int value = 0;
void increment() {
value = value + 1;
}
void add(int amount) {
value = value + amount;
}
int getValue() {
return value;
}
public static void main(String[] args) {
Counter first = new Counter();
Counter second = new Counter();
first.increment();
first.add(2);
System.out.println(first.getValue());
System.out.println(second.value);
}
}Compile and run it from the folder containing the file:
javac Counter.java
java CounterThe output is:
3
0The main method is where this program starts. For now, use the two lines with new Counter() to make the objects. You will explore object creation in the next chapter.
Follow the state changes
The class declares one field:
int value = 0;The type int holds whole numbers. The field starts at 0 for each counter object.
The program makes two counters, named first and second. Each has its own value field. Changing one counter does not change the other.
The dot in first.increment() selects a method on first. The parentheses call, or run, that method. Inside increment, the line value = value + 1 changes the field of the counter on which the method was called.
Follow the first counter:
- Its
valuestarts at0. first.increment()changes it to1.first.add(2)changes it to3.
Nothing changes the second counter, so its value stays 0.
Give methods information and get results
The add method takes an argument, a value supplied in a method call. In first.add(2), the argument is 2.
Inside the method, amount is a parameter: a variable that receives the argument. The method adds amount to the counter’s field.
The word void means that increment and add do not return a value. They change the counter’s state instead.
The getValue method has int before its name. That is its return type, which says what kind of value the method sends back. The statement return value; sends the current field value back to the caller.
The last print statement uses second.value to read the field directly. The first uses first.getValue() to read it through a method. In both cases, the dot identifies which counter to use.
Fields and local variables
A field is declared in the class, outside its methods. A local variable is declared inside a method and is used there. In the example, value is a field, while first and second are local variables in main. A parameter such as amount is also used inside its method.
The example sets value to 0 when it declares the field. If an int field has no initial value written in the class, Java gives it 0. Local variables are different: you must assign them a value before you read them.
Exercise
After the existing statements in main, you call first.increment() and second.add(4). What is each counter’s value?
Show answer
first is 4, and second is 4. Each method changes the field of the counter on which it was called.