Sub topic 4
Compact Source Files
This chapter assumes you know basic Java variables, methods, and control flow. You should also know what a class, field, and method are, and how to compile and run a traditional Java program from the command line.
A compact source file is a Java source file that contains members without declaring a named top-level class. Java creates an implicitly declared class around those members. This lets a small program focus on its behavior instead of its class declaration.
The traditional structure
A traditional Java program places its main method inside a class:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}This program is valid, but a beginner must understand several things before seeing the output:
public class Hellodeclares a class.staticmeans the method belongs to the class rather than an object.voidmeans the method returns no value.String[] argsis an array of command-line arguments.System.outrefers to the standard output stream.
A compact source file removes most of this setup:
void main() {
IO.println("Hello, world!");
}Save this code in a file named Hello.java. The method is a member of an implicitly declared class named Hello.
What Java supplies
The compiler treats the compact source file as if it had an enclosing class. Conceptually, the result is similar to this:
class Hello {
void main() {
IO.println("Hello, world!");
}
}The conceptual version is useful for understanding the structure. You do not write this wrapper in the compact file.
The launcher recognizes the no-argument instance method void main(). It creates an instance of the implicit class and invokes that method. The method does not need public, static, or a String[] parameter.
Adding fields and helper methods
Methods and fields written at the top level become members of the implicit class. They work together as instance members:
String greeting = "Hello";
String greet(String name) {
return greeting + ", " + name + "!";
}
void main() {
IO.println(greet("Java"));
}The field greeting belongs to each instance. The method greet reads that field and returns a string. When the launcher calls main, main calls greet on the same object.
This structure is useful for a small teaching example or a short utility. It also allows you to introduce methods and state without first explaining every class declaration detail.
Imports and helper types
Compact source files automatically import the packages exported by the java.base module. For example, List can be used without an explicit import:
void main() {
List<String> names = List.of("Ada", "Grace");
for (String name : names) {
IO.println(name.toUpperCase());
}
}You can also declare helper types in the same file. A record is a compact way to represent data:
record Point(int x, int y) {}
void main() {
IO.println(new Point(1, 2));
}The record becomes a member of the implicit class. The output is:
Point[x=1, y=2]When to use one
Use a compact source file for a small program, experiment, classroom example, or script-like utility. When the program needs several collaborating classes, a public API, inheritance design, or detailed access control, convert it to an explicit class structure.
The conversion is mechanical. Move the fields and methods inside a named class, then choose the appropriate main signature for the way you want to launch the program.
Exercise
What implicit class name does Java use for a compact source file saved as Report.java?
Show answer
Java uses Report as the name of the implicitly declared class.