Writing a main() Method – Building Blocks – 1Z0-829 Study Guide

Writing a main() Method

A Java program begins execution with its main() method. In this section, you learn how to create one, pass a parameter, and run a program. The main() method is often called an entry point into the program, because it is the starting point that the JVM looks for when it begins running a new program.

Creating a main() Method

The main() method lets the JVM call our code. The simplest possible class with a main()

method looks like this:

  1. public class Zoo {
  • public static void main(String[] args) {
  • System.out.println(“Hello World”);
  • }
  • }

This code prints Hello World. To compile and execute this code, type it into a file called

Zoo.java and execute the following:

javac Zoo.java

java Zoo

If it prints Hello World, you were successful. If you do get error messages, check that you’ve installed the Java 17 JDK, that you have added it to the PATH, and that you didn’t make any typos in the example. If you have any of these problems and don’t know what to do, post a question with the error message you received in the Beginning Java forum at CodeRanch:

www.coderanch.com/forums/f-33/java

To compile Java code with the javac command, the file must have the extension .java. The name of the file must match the name of the public class. The result is a file of bytecode with the same name but with a .class filename extension. Remember that bytecode consists of instructions that the JVM knows how to execute. Notice that we must omit the .class extension to run Zoo.class.

The rules for what a Java file contains, and in what order, are more detailed than what we have explained so far (there is more on this topic later in the chapter). To keep things simple for now, we follow this subset of the rules:

Each file can contain only one public class.

The filename must match the class name, including case, and have a .java extension.

If the Java class is an entry point for the program, it must contain a valid main() method.

Let’s first review the words in the main() method’s signature, one at a time. The keyword public is what’s called an access modifier. It declares this method’s level of exposure to potential callers in the program. Naturally, public means full access from anywhere in the program. You learn more about access modifiers in Chapter 5.

The keyword static binds a method to its class so it can be called by just the class name, as in, for example, Zoo.main(). Java doesn’t need to create an object to call the main() method—­which is good since you haven’t learned about creating objects yet! In fact, the JVM does this, more or less, when loading the class name given to it. If a main() method doesn’t have the right keywords, you’ll get an error trying to run it. You see static again in Chapter 6, “Class Design.”

The keyword void represents the return type. A method that returns no data returns con-trol to the caller silently. In general, it’s good practice to use void for methods that change an object’s state. In that sense, the main() method changes the program state from started to fin-ished. We explore return types in Chapter 5 as well. (Are you excited for Chapter 5 yet?)

Finally, we arrive at the main() method’s parameter list, represented as an array of java.lang.String objects. You can use any valid variable name along with any of these three formats:

String[] args

String options[]

String… friends

The compiler accepts any of these. The variable name args is common because it hints that this list contains values that were read in (arguments) when the JVM started. The char-acters [] are brackets and represent an array. An array is a fixed-­size list of items that are all of the same type. The characters … are called varargs (variable argument lists). You learn about String in this chapter. Arrays are in Chapter 4, “Core APIs,” and varargs are in Chapter 5.

Optional Modifiers in main() Methods

While most modifiers, such as public and static, are required for main() methods, there are some optional modifiers allowed.

public final static void main(final String[] args) {}

In this example, both final modifiers are optional, and the main() method is a valid entry point with or without them. We cover the meaning of final methods and parameters in Chapter 6.