ch7 - Defining Own Class
ch7 - Defining Own Class
Separating the class into sections is done for clarity. It is not a rule of the language. A
simple class might have just a few variables and be defined in just a few lines of code. A
large, complicated class might take thousands of lines of code for its definition.
A Tiny Example
The returnType is the type of value that the method hands back to the caller of
the method. Methods in classes you define can return values just as do methods from
library classes. The return statement is used to hand back a value to the caller.
If you want a method that does something, but does not return a value to the caller, use a
return type of void and do not use a return value with the return statement.
The return statement can be omitted; the method will automatically return to the caller
after it executes. Here is the method from the example program:
// method definition
void speak() {
System.out.println("Hello from an object!");
}