Читать книгу OCP Oracle Certified Professional Java SE 17 Developer Study Guide - Jeanne Boyarsky - Страница 68

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 { 2: public static void main(String[] args) { 3: System.out.println("Hello World"); 4: } 5: }

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 control 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 finished. 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 characters [] 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.

OCP Oracle Certified Professional Java SE 17 Developer Study Guide

Подняться наверх