Sub topic 5
The Class the Compiler Declares for You
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 run a Java source file from the command line.
A compact source file can contain methods and fields without an explicit class declaration:
String greeting = "Hello";
void main() {
IO.println(greeting + ", Java!");
}This code is still a class-based Java program. Java does not remove the class. Instead, the compiler declares a class for you.
Implicitly declared classes
An implicitly declared class is the class that Java creates around the members of a compact source file. The class name comes from the source file name in ordinary file-based use. For example, a file named Hello.java produces an implicit class named Hello.
Conceptually, Java treats the compact source file like this:
public class Hello {
String greeting = "Hello";
void main() {
IO.println(greeting + ", Java!");
}
}The compiler does not need to copy and paste these characters literally. The important point is the meaning: greeting becomes a field, and main becomes a method of the implicit class.
The method is an instance method. An instance method belongs to an object created from a class. When the Java launcher finds an instance main method, it creates an object and invokes that method on the object.
Members belong to the implicit class
Methods and fields written at the top level become members of the implicit class. Therefore, one method can call another method, and a method can read or update a field.
String greeting = "Hello";
String greet(String name) {
return greeting + ", " + name + "!";
}
void main() {
IO.println(greet("Ada"));
}The call to greet("Ada") works because both methods belong to the same object. The method can also access the greeting field through that object.
This is equivalent in structure to:
public class Greeting {
String greeting = "Hello";
String greet(String name) {
return greeting + ", " + name + "!";
}
void main() {
IO.println(greet("Ada"));
}
}The compact form removes the outer class declaration, but it does not change the basic object model.
What the implicit class is not
The implicit class is not a package. It is not a special kind of object that replaces ordinary Java classes. It is an ordinary class representation created from a shorter source form.
The source file can also contain helper types. For example, a record can be declared alongside the top-level method:
record Point(int x, int y) {}
void main() {
IO.println(new Point(1, 2));
}The Point record becomes a member type of the implicit class. Its use remains familiar: new Point(1, 2) creates a record object.
When the hidden class becomes visible
The hidden class is useful while a program is small. As the program grows, you may want an explicit class declaration. This makes the class name, access level, constructors, and member organization visible to readers and tools.
Converting the compact version does not require changing the method bodies. Add a class declaration and move the members inside it:
public class Greeting {
String greeting = "Hello";
String greet(String name) {
return greeting + ", " + name + "!";
}
void main() {
IO.println(greet("Ada"));
}
}The behavior stays the same. The main difference is that the class boundary is now written in the source code.
Exercise
In a compact source file named Greeting.java, where does the field greeting belong, and what happens when the launcher finds void main()?
Show answer
greeting becomes a field of the implicit Greeting class. The launcher creates a Greeting object and calls its instance main method.