1.2. Executable Java applications

[1.3] Create executable Java applications with a main method; run a Java program from the command line; including console output.

The OCA Java SE 8 Programmer I exam requires that you understand the meaning of an executable Java application and its requirements, that is, what makes a regular Java class an executable Java class. You also need to know how to execute a Java program from the command line.

1.2.1. Executable Java classes versus non-executable Java classes

Doesn’t the Java Virtual Machine execute all the Java classes when they are used? If so, what is a non-executable Java class?

An executable Java class, when handed over to the JVM, starts its execution at a particular point in the class—the main method. The JVM starts executing the code that’s defined in the main method. You can’t hand over a non-executable Java class (class without a main method) to the JVM and ask it to execute it. In this case, the JVM won’t know which method to execute because no entry point is marked.

Typically, an application consists of a number of classes and interfaces that are defined in multiple Java source code files. Of all these files, a programmer designates one of the classes as an executable class. The programmer can define the steps that the JVM should execute as soon as it launches the application. For example, a programmer can define an executable Java class that includes code to display the appropriate GUI window to a user and to open a database connection.

In figure 1.6, the classes Window, UserData, ServerConnection, and UserPreferences don’t define a main method. Class LaunchApplication defines a main method and is an executable class.

Figure 1.6. Class LaunchApplication is an executable Java class, but the rest of the classes—Window, UserData, ServerConnection, and UserPreferences—aren't.

Note

A Java application can define more than one executable class. We choose one (and exactly one) when the time comes to start its execution by the JVM.

1.2.2. The main method

The first requirement in creating an executable Java application is to create a class with a method whose signature (name and method arguments) matches the main method, defined as follows:

public class HelloExam {
    public static void main(String args[]) {
        System.out.println("Hello exam");
    }
}

This main method should comply with the following rules:

  • The method must be marked as a public method.
  • The method must be marked as a static method.
  • The name of the method must be main.
  • The return type of this method must be void.
  • The method must accept a method argument of a String array or a variable argument (varargs) of type String.

Figure 1.7 illustrates the previous code and its related set of rules.

Figure 1.7. Ingredients of a correct main method

It’s valid to define the method parameter passed to the main method as a variable argument (varargs) of type String:

To define a variable argument variable, the ellipsis (...) must follow the type of the variable and not the variable itself (a mistake made by a lot of new programmers):

As mentioned previously, the name of the String array passed to the main method need not be args to qualify it as the correct main method. The following examples are also correct definitions of the main method:

To define an array, the square brackets [] can follow either the variable name or its type. The following is a correct method declaration of the main method:

It’s interesting to note that the placement of the keywords public and static can be interchanged, which means that the following are both correct method declarations of the main method:

Note

Though both public static and static public are the valid order of keywords to declare the main method, public static is more common and thus more readable.

On execution, the code shown in figure 1.7 outputs the following:

Hello exam

If a class defines a main method that doesn’t match the signature of the main method, it’s referred to as an overloaded method (overloaded methods are discussed in detail in chapter 3). Overloaded methods are methods with the same name but different signatures. For a quick example, class HelloExam can define multiple methods with the method name main:

On execution, JVM will execute the main method, resulting in the output Hello exam.

1.2.3. Run a Java program from the command line

Almost all Java developers work with an Integrated Development Environment (IDE). This exam, however, expects you to understand how to execute a Java application, or an executable Java class, using the command prompt. For this reason, I suggest you work with a simple text editor and command line (even if this might never be the approach you use in the real world).

Note

If you need help getting your system set up to compile or execute Java applications using the command prompt, refer to Oracle’s detailed instructions at http://docs.oracle.com/javase/tutorial/getStarted/cupojava/index.html.

Let’s revisit the code shown in figure 1.7:

public class HelloExam {
    public static void main(String args[]) {
        System.out.println("Hello exam");
    }
}

To execute the preceding code using a command prompt, issue the command java HelloExam, as shown in figure 1.8.

Figure 1.8. Using the command prompt to execute a Java application

I mentioned that the main method accepts an array of String as the method parameter. But how and where do you pass the array to the main method? Let’s modify the previous code to access and output values from this array:

public class HelloExamWithParameters {
    public static void main(String args[]) {
        System.out.println(args[0]);
        System.out.println(args[1]);
    }
}

Now let’s execute the preceding code using the command prompt, as shown in figure 1.9.

Figure 1.9. Passing command parameters to a main method

As you can see from the output shown in figure 1.9, the keyword java and the name of the class aren’t passed as command parameters to the main method. The OCA Java SE 8 Programmer I exam will test you on your knowledge of whether the keyword java and the class name are passed on to the main method.

Exam Tip

The method parameters that are passed to the main method are also called command-line parameters or command-line values. As the name implies, these values are passed to a method from the command line.

If you weren’t able to follow the code with respect to the arrays and the class String, don’t worry; we’ll cover the class String and arrays in detail in chapter 4.

Here’s the next Twist in the Tale exercise for you. In this exercise, and in the rest of the book, you’ll see the names Shreya, Harry, Paul, and Selvan, who are hypothetical programmers also studying for this certification exam. The answer is provided in the appendix, as usual.

Twist in the Tale 1.3

One of the programmers, Harry, executed a program that gave the output java one. Now he’s trying to figure out which of the following classes outputs these results. Given that he executed the class using the command java EJava java one one, can you help him figure out the correct option(s)?

  1. class EJava {
        public static void main(String sun[]) {
            System.out.println(sun[0] + " " + sun[2]);
        }
    }
  2. class EJava {
        static public void main(String phone[]) {
            System.out.println(phone[0] + " " + phone[1]);
        }
    }
  3. class EJava {
        static public void main(String[] arguments[]) {
            System.out.println(arguments[0] + " " + arguments[1]);
        }
    }
  4. class EJava {
        static void public main(String args[]) {
            System.out.println(args[0] + " " + args[1]);
        }
    }
Confusion with command-line parameters

If you’ve programmed in languages like C, you might get confused by the command-line parameters. Programming languages like C pass the name of a program as a command-line argument to the main method. But Java doesn’t pass the name of the class as an argument to the main method.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.235.144