Скачать книгу

      Most of the time, each Java class is defined in its own .java file. In this chapter, the only top-level type is a class. A top-level type is a data structure that can be defined independently within a source file. For the majority of the book, we work with classes as the top-level type, but in Chapter 7, “Beyond Classes,” we present other top-level types, as well as nested types.

      A top-level class is often public, which means any code can call it. Interestingly, Java does not require that the type be public. For example, this class is just fine:

      1: class Animal { 2: String name; 3: }

      You can even put two types in the same file. When you do so, at most one of the top-level types in the file is allowed to be public. That means a file containing the following is also fine:

      1: public class Animal { 2: private String name; 3: } 4: class Animal2 {}

      If you do have a public type, it needs to match the filename. The declaration public class Animal2 would not compile in a file named Animal.java. In Chapter 5, “Methods,” we discuss what access options are available other than public.

      Note Icon Noticing a pattern yet? This chapter includes numerous references to topics that we go into in more detail in later chapters. If you're an experienced Java developer, you'll notice we keep a lot of the examples and rules simple in this chapter. Don't worry; we have the rest of the book to present more rules and complicated edge cases!

      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 { 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 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.

      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.

      Passing Parameters to a Java Program

      Let's see how to send data to our program's main() method. First, we modify the Zoo program to print out the first two arguments passed in:

      The code args[0] accesses the first element of the array. That's right: array indexes begin with 0 in Java. To run it, type this:

      javac Zoo.java java Zoo Bronx

Скачать книгу