Sub topic 11
Growing a Script into a Full Class
Assume you know basic Java variables, methods, fields, classes, and the traditional public static void main(String[] args) method. This chapter uses Java 25, where instance main methods and compact source files are final features.
A compact source file is useful when a program is small. You write methods and fields without writing the surrounding class declaration. Java creates an implicitly declared class for those members.
As the program grows, an explicit class gives you a clearer structure. You can name the class in the source, choose its access level, add constructors, and organize related types more deliberately.
The starting script
This compact source file stores a count and prints a greeting:
private int count;
void main() {
count++;
IO.println("Greeting number " + count);
}Save it as Greeter.java. The compiler treats the file as if it contained a class named Greeter. The count field and main method become members of that class.
The main method is an instance method. Before calling it, the Java launcher creates a Greeter object with its accessible no-argument constructor.
Making the class explicit
To grow this script into an ordinary class, wrap its members in a class declaration:
public class Greeter {
private int count;
void main() {
count++;
IO.println("Greeting number " + count);
}
}This version has the same behavior. The launcher can still create a Greeter object and call its instance main method:
javac Greeter.java
java GreeterThe first run prints:
Greeting number 1The public modifier makes the class accessible from outside its package. The class name must match the file name when the class is public. Therefore, public class Greeter belongs in Greeter.java.
| Compact source file | Explicit class |
|---|---|
| Java supplies the surrounding class | You write the class declaration |
| Members are usually instance members | You choose instance or static members |
| Convenient for a small program | Better for structure and growth |
| No constructor declaration | Constructors can be declared |
Adding constructors and helpers
An explicit class lets you control object creation. For example, the counter can receive an initial value:
public class Greeter {
private int count;
private final String language;
public Greeter(String language) {
this.language = language;
}
void main() {
count++;
IO.println(language + " greeting number " + count);
}
String message(String name) {
return "Hello, " + name + "!";
}
}This example no longer has an accessible no-argument constructor. The launcher requires an accessible no-argument constructor for an instance main, so java Greeter cannot launch this class as written. Add a no-argument constructor if the launcher should create it:
public Greeter() {
this("English");
}A constructor is a special method that runs when an object is created. Once you add any constructor, Java no longer supplies a default no-argument constructor automatically.
What changes and what does not
Moving members into an explicit class does not automatically change their instance behavior. A method can still access instance fields without writing an object name. The important change is that you now manage the class boundary yourself.
You can also declare nested helper classes, records, or interfaces inside the explicit class. In a compact source file, such declarations become members of the implicitly declared class. When the file needs multiple independent top-level classes, separate files are usually clearer.
Exercise
Why might adding a constructor stop java Greeter from launching an instance main method?
Show answer
The launcher creates the instance before calling main. If the class has no accessible no-argument constructor, the launcher cannot create that instance.