6 Implicitly Declared Classes and Instance Main Methods
The features Instance Main Methods and Implicitly Declared Classes enable students to write their first programs without needing to understand the full set of language features designed for large programs.
Note:
This is a preview feature. A preview feature is a feature whose design, specification, and implementation are complete, but is not permanent. A preview feature may exist in a different form or not at all in future Java SE releases. To compile and run code that contains preview features, you must specify additional command-line options. See Preview Language and VM Features.
For background information about instance main methods and implicitly declared classes, see JEP 477.
The Java programming language excels in developing large, complex applications developed and maintained over many years by large teams. It has rich features for data hiding, reuse, access control, namespace management, and modularity which allow components to be cleanly composed while being developed and maintained independently. The composition of large components is called programming-in-the-large.
However, the Java programming language is also intended to be a first language and offers many constructs that are useful for programming-in-the-small (everything that is internal to a component). When programmers first start out, they do not write large programs in a team — they write small programs by themselves. At this stage, there is no need for the programming-in-the-large concepts of classes, packages, and modules.
When teaching programming, instructors start with the basic programming-in-the-small concepts of variables, control flow, and subroutines. There is no need for the programming-in-the-large concepts of classes, packages, and modules. Students who are learning to program have no need for encapsulation and namespaces which are useful later to separately evolve components written by different people.
- Enhance the protocol by which Java programs are launched by allowing
instance main methods that are not
static
, need not bepublic
, and need not have aString[]
parameter. See Flexible Launch Protocol. - Allow a compilation unit (a source file) to implicitly declare a class. See 7.3. Compilation Units in the Java Language Specification and Implicitly Declared Classes.
- Automatically import methods for simple textual I/O with the console and public top-level classes and interfaces of the packages exported by the java.base module. See Automatic Import of the Static Methods of java.io.IO and the Module java.base.
HelloWorld
program that is often used
as the first program for Java students:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
In this first program:
- The
class
declaration and the mandatorypublic
access modifier are programming-in-the-large constructs. They are useful when encapsulating a code unit with a well-defined interface to external components, but rather pointless in this little example. - The
String[] args
parameter also exists to interface the code with an external component, in this case the operating system's shell. It is mysterious and unhelpful here, especially since it is not used in simple programs likeHelloWorld
. - The
static
modifier is part of Java's class-and-object model. For the novice,static
is not just mysterious but also harmful. To add more methods or fields thatmain
can call and use, the student must either declare them all asstatic
(propagating an idiom which is neither a common nor a good habit) or else confront the difference between static and instance members and learn how to instantiate an object.
Instance main methods and implicitly declared classes reduce the complexity
of writing simple programs such as HelloWorld
by enabling programmers
to write programs without using access modifiers, static
modifiers, or
the String[]
parameter. Far from being a separate dialect, students can
now use the Java language to write streamlined declarations for single-class programs
and then later seamlessly expand their beginning programs to include more advanced
features as their skills grow. Java veterans might also find that instance main methods
and implicitly declared classes are useful features when writing simple Java programs
that do not require the programming-in-the-large scaffolding of the Java language. The
introduction of programming-in-the-large constructs can be postponed by instructors
until they are needed.
Flexible Launch Protocol
In the JDK, the launch protocol is implemented by the command-line tool
as the java
executable.
The actions of choosing the class containing the main
method, assembling its dependencies in the form of a module path or a class path (or
both), loading the class, initializing it, and invoking the main
method
with its arguments constitute the launch protocol. With JEP 463, the launch protocol has
been enhanced to offer more flexibility in the declaration of a program's entry point
and, in particular, to allow instance main
methods, as follows:
- Allows the
main
method of a launched class to havepublic
,protected
, or default (such as package) access. - If a launched class contains a
main
method with aString[]
parameter, then it chooses that method. - If a launched class contains a
main
method without parameters, then it chooses that method. - In either case, if the chosen
main
method isstatic
then it invokes it. -
Otherwise, the chosen
main
method is an instancemain
method and the launched class must have a zero-parameter, non-private
constructor (public
,protected
, or package access). It invokes that constructor and then invokes themain
method of the resulting object. If there is no such constructor, then it reports an error and terminates. -
If there is no suitable
main
method then it reports an error and terminates.
main
methods, we can simplify the
HelloWorld
program presented in Implicitly Declared Classes and Instance Main Methods
to:class HelloWorld {
void main() {
System.out.println("Hello, World!");
}
}
Implicitly Declared Classes
In the Java language, every class resides in a package and every package resides in a module. These namespacing and encapsulation constructs apply to all code. However, small programs that don't need them can omit them.
A program that doesn't need class namespaces can omit the
package
statement, making its classes implicit members of the
unnamed package. Classes in the unnamed package cannot be referenced explicitly by
classes in named packages. A program that doesn't need to encapsulate its packages can
omit the module declaration, making its packages implicit members of the unnamed module.
Packages in the unnamed module cannot be referenced explicitly by packages in named
modules.
Before classes serve their main purpose as templates for the construction of objects, they serve as namespaces for methods and fields. We should not require students to confront the concept of classes:
-
Before they are comfortable with the basic building blocks of variables, control flow, and subroutines,
-
Before they embark on learning object orientation, and
-
When they are still writing simple, single-file programs.
Even though every method resides in a class, we can stop requiring explicit class declarations for code that doesn't need it — just as we don't require explicit package or module declarations for code that don't need them.
Beginning with JEP 463, when the Java compiler encounters a source file containing a method not enclosed in a class declaration, it considers that method, any similar methods, and any unenclosed fields and classes in the file to form the body of an implicitly declared top-level class.
An implicitly declared class (also known as an implicit class) is always a
member of the unnamed package. It is also final
and doesn't implement
any interface or extend any class other than Object
. An implicit class
can't be referenced by name, so there can be no method references to its static methods.
However, the this
keyword can still be used, as well as method
references to instance methods.
The code of an implicit class can't refer to the implicit class by name, so
instances of an implicit class can't be constructed directly. Such a class is useful
only as a standalone program or as an entry point to a program. Therefore, an implicit
class must have a main
method that can be launched as described in
Flexible Launch Protocol. This requirement is enforced by the Java compiler.
An implicit class resides in the unnamed package, and the unnamed package
resides in the unnamed module. While there can only be one unnamed package (barring
multiple class loaders) and only one unnamed module, there can be multiple implicit
classes in the unnamed module. Every implicit class contains a main
method and represents a program. Consequently, multiple implicit classes in an unnamed
package represent multiple programs.
An implicit class is similar to an explicitly declared class. Its members can
have the same modifiers (such as private
and static
)
and the modifiers have the same defaults (such as package
access and
instance membership). One key difference is that while an implicit class has a default
zero-parameter constructor, it can have no other constructor.
HelloWorld
program
as:void main() {
System.out.println("Hello, World!");
}
String greeting() { return "Hello, World!"; }
void main() {
System.out.println(greeting());
}
String greeting = "Hello, World!";
void main() {
System.out.println(greeting);
}
HelloWorld.java
that
contains an implicit class with the java
command-line tool as
follows:$ java HelloWorld.java
The Java compiler compiles that file to the launchable class file
HelloWorld.class
. In this case, the compiler chooses
HelloWorld
for the class name as an implementation detail. However,
that name still can't be used directly in Java source code.
At this time, the javadoc
tool can't generate API
documentation for an implicit class because implicit classes don't define an API that is
accessible from other classes. However, fields and methods of an implicit class can
generate API documentation.
Automatic Import of the Static Methods of java.io.IO and the Module java.base
Every implicitly declared class automatically imports the static methods of the class
java.io.IO
:
- println(Object): Writes a string representation of the specified object to the system console, and then flushes that console.
- print(Object): Writes a string representation of the specified object to the system console, terminates the line, and then flushes that console.
- readln(String): Writes a prompt as if calling print, and then reads a single line of text from the system console.
It is as if the static-import-on-demand declaration import
java.io.IO.*
appears at the beginning of every implicitly declared
class.
Consequently, you can further simplify the HelloWorld
program as:
String greeting = "Hello, World!";
void main() {
println(greeting);
}
In addition, every implicitly declared class imports, on demand, all public
top-level classes and interfaces in all packages exported by the java.base module. It is as if the module import declaration
import module java.base
appears at the beginning of every
implicitly declared class. See Module Import Declarations for more information.
The following example is an implicitly declared class that requires no import declarations for Map, Stream, Collectors, or Function as they are contained in packages exported by the java.base module.
void main() {
String[] fruits = new String[] { "apple", "berry", "citrus" };
Map<String, String> m = Stream
.of(fruits)
.collect(Collectors.toMap(
s -> s.toUpperCase().substring(0,1),
Function.identity()));
m.forEach((k, v) -> println(k + " " + v));
}
Growing a Program
By omitting the concepts and constructs it doesn't need, a
HelloWorld
program written as an implicit class is more focused on what
the program actually does. Even so, all members continue to be interpreted just as they are
in an ordinary class.
Concepts and constructs can easily be added to an implicit class as needed by
the program. To evolve an implicit class into an ordinary class, all we need to do is
wrap its declaration, excluding import
statements, inside an explicit
class
declaration.