Demystifying “public static void main()”: Building a “Hello World” Java Application
By Alejandro Gervasio October 15, 2018 Java Core
Overview
The Java language is a neat example of the implementation of the OOP (object-oriented programming) paradigm. While it's fair to admit that mastering the paradigm's foundations is in most cases a difficult process, funny enough, Java itself is fairly easy to learn.
In this introductory article, we'll learn how to develop from scratch a simple Java application that prints out to the console the classic “Hello World” message.
Defining the “HelloWorldApplication” Class
To keep things simple, we'll create our example “Hello World” application without relying on any IDE in particular.
Therefore, let’s launch our preferred text editor, (e.g. NotePad on Windows or Text Edit on Mac), and write the following code:
public class HelloWorldApplication {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Now, let's save the code snippet in a file called HelloWorldApplication.java, in the location of our choice in the file system.
While at first sight the example looks pretty naive, it actually covers a lot of background. Thus, let's break it down in several parts, so we can understand more clearly how it works:
- The HelloWorldApplication class: we defined it with the public access modifier preceding the class keyword and the class name. This means that the class is public, hence we can access it from anywhere within the application, with no restrictions whatsoever.
- The main() method is actually the sample application’s entry point. The public access modifier declares the method public, while that the static keyword implies that there’s no need to instantiate the class to call it. Lastly, the void keyword signals that the method returns no value(s).
- The main() method arguments, String[] args: an array of strings, which we can use for passing command-line arguments to the method.
- The System.out.println statement does exactly what we might expect: it prints out to the console the “Hello World!” text.
Compiling the “HelloWorldApplication” Class
At this point, HelloWorldApplication.java is just a plain text file with a fancy extension. To make it an executable file, though, first we need to compile it.
Compiling a Java file means basically converting the source code to bytecode, a format which can be interpreted and executed by the JVM (Java Virtual Machine).
Therefore, to compile our source file, let’s open up a command prompt. Next, let’s navigate to the location in the file system where we previously saved the HelloWorldApplication.java file, and type the javac command, followed by the name of the source file (the .java extension is required):
javac HelloWorldApplication.java
The javac command reads Java classes and interface definitions, and compiles them into bytecode class files. In addition, it can also process annotations in Java source files and classes.
Once we have run the javac command, a new executable HelloWorldApplication.class file should have been created in the same location of HelloWorldApplication.java.
Running the Executable File
Assuming that no compilation errors were triggered during the compiling phase, then we can now run the application by typing the java command, followed by the name of the executable file:
java HelloWorldApplication
The java command runs a Java program from a command prompt. When we invoke the java command, the JRE (Java Runtime Environment) is loaded, along with the supplied class. Finally, the class' main() method is executed.
In this case, the HelloWorldApplication class' main() method will be called. As a result, we should see the “Hello World!” text printed out in the console:
Hello World!
Further Improvements
Of course, there's plenty of room to improve the existing functionality of the HelloWorldApplication class.
We could refactor, for instance, the main() method, so it can take one or more command-line arguments and print them out to the console, along with the “Hello World!” message:
public static void main(String[] args) {
for (int i = 0; i < args.length; i++) {
System.out.println(args[i] + "says Hello World!");
}
}
Likewise, we should first recompile the HelloWorldApplication.java file by using the javac command, and then rerun the class file by passing the command-line arguments to the java command:
java HelloWorldApplication John Heather Jennifer
As expected, the refactored example will generate the following output:
John says Hello World!
Heather says Hello World!
Jennifer says Hello World!
Summary
In this tutorial, we learned how to build from scratch a basic Java application. We deliberately kept all the examples easy to understand, as we put strong emphasis on learning a few key concepts of the Java language.
Here's a quick summary of them:
- Class: the de-facto unit of data and behavior in Java, and the blueprint for objects. Objects or class instances can maintain internal state through one or more fields, and implement behavior via one or multiple methods.
- The public access modifier: it allows us to declare the public visibility of class fields and methods.
- Method or function: a code block container, which we can use to encapsulate behavior within a certain class.
- Array: a dynamically-created container object that holds a fixed number of elements of the same type.