<< Chapter < Page Chapter >> Page >

Output produced by the system

Figure 2 not only shows the output produced by the program. It also shows information produced by the Java compiler and the Java virtual machine as a result of executing the following commands at runtime:

javac -version java -version

Discussion and sample code

Will explain in fragments

I will explain this program in fragments. A complete listing of the program is provided in Listing 9 near the end of the module.

I will begin with the driver class named Prob01 , which is shown in its entirety in Listing 1 .

Listing 1 . The driver class.
import java.awt.Color; public class Prob01{//Driver classpublic static void main(String[] args){//Instantiate an object and call its method named run. Prob01Runner obj = new Prob01Runner();obj.run(); //Get information from the object and display it on// the command-line screen. System.out.println(obj.getMars());System.out.println(obj.getJoe()); System.out.println(obj.getSue());}//end main }//end class Prob01

The import directive

Note the import directive at the very beginning of Listing 1 . This is a directive to the compiler and the virtual machine notifying them that the class named Color can be found in the package named java.awt .

What is a package?

Boiled down to its simplest description, a package is nothing more than the specification of a particular folder on the disk relative to a standard root folder. (Think of it as a disk-path specification with the periods representing \ characters on a Windows machine and representing / characters on a Unix machine.)

The public class named Prob01

Every Java application (not true for Java applets) must include the definition of a class that contains the definition of a method named main with a method signature having the syntax shown in Listing 1 .

The name of the class/application

The name of the class containing the main method is also the name of the application insofar as being able to compile and execute the application is concerned. In this case, the name of the class, and hence the name of the application is Prob01 .

Source code file name

The name of the source code file containing this class must match the name of the class. In this case, the source code file must be named Prob01.java . (Note the .java extension.)

Compiling the application

In its simplest form, this application can be compiled by executing the following command at the command prompt:

javac Prob01.java

Note however that it is often necessary to specify the path to various library files on the command line when compiling the application. In that case, the simplest form given above is not sufficient.

Compiler output file names

When the application is successfully compiled, it will produce one or more output files with an extension of .class . In this case, one of those files will be named Prob01.class .

Execution of the application

The execution of C and C++ programs begins and ends in the main function. The execution of Java applications begin and end in the method named main .

Once again, in its simplest form, this application can be executed by entering the following command at the command prompt:

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask