Sub topic 6
Top-Level Methods and Fields
This chapter assumes you know Java variables, methods, fields, classes, and the traditional public static void main(String[] args) method. You should also know how to compile and run a Java program.
A compact source file lets you write methods and fields without first writing a class declaration. Java places those declarations inside an implicitly declared class. The methods and fields are still ordinary class members. They are not truly outside a class.
Declaring a field and methods
The following compact source file declares a field named greeting and a method named greet:
String greeting = "Hello";
String greet(String name) {
return greeting + ", " + name + "!";
}
void main() {
IO.println(greet("Java"));
}When you run this file, it prints:
Hello, Java!The field stores shared state for the object created to run the program. The greet method reads that field and returns a string. The main method calls greet.
Inside greet, the name greeting means the current object's field. You could write the same code explicitly with this:
String greet(String name) {
return this.greeting + ", " + name + "!";
}Here, this refers to the object whose method is currently running.
How the members fit together
Java treats the compact file approximately as if it were wrapped in a class:
class GreetingProgram {
String greeting = "Hello";
String greet(String name) {
return greeting + ", " + name + "!";
}
void main() {
IO.println(greet("Java"));
}
}The actual implicit class has a name based on the source file. You normally do not write or reference that name in the compact source file.
The launcher creates an instance of the class, then calls its instance main method. The same object contains the greeting field and the greet method.
Fields preserve state
A field can change while the program runs. This is useful for small examples that need simple state:
private int count;
void recordGreeting(String name) {
count++;
IO.println(count + ": Hello, " + name);
}
void main() {
recordGreeting("Ada");
recordGreeting("Grace");
}The output is:
1: Hello, Ada
2: Hello, Gracecount starts at its default value of 0. Each call to recordGreeting increases the same field on the same object.
The private modifier restricts direct access to the field from outside the class. You can use ordinary Java access modifiers on members in a compact source file.
| Declaration | Role |
|---|---|
| `String greeting = "Hello";` | An instance field that stores data |
| `String greet(String name)` | An instance method that returns data |
| `void main()` | The instance method selected as the program entry point |
Common errors
A top-level method is not a free-standing function. It belongs to the implicit class, so it follows normal Java member rules.
For example, a method cannot use a local variable declared inside another method:
void main() {
String punctuation = "!";
printGreeting();
}
void printGreeting() {
// punctuation is not visible here
}Move the value to a field if both methods need it:
String punctuation = "!";
void main() {
printGreeting();
}
void printGreeting() {
IO.println("Hello" + punctuation);
}Use a local variable when only one method needs the value. Use a field when the value represents object state shared by several instance methods.
Exercise
In the following program, what does the second call print, and why? int total; void add(int value) { total += value; IO.println(total); } void main() { add(3); add(4); }
Show answer
It prints 7. Both calls use the same total field on the same launched object. The first call changes it from 0 to 3, and the second changes it from 3 to 7.