This page is optimized for mobile devices, if you would prefer the desktop version just click here

3.2 Java3002: creating and manipulating turtles and pictures in a  (Page 3/12)

java Prob01

Again, it is often necessary to specify the path to various library files on the command line when executing the application. In that case, the simplest form is not sufficient.

Compilation and execution on my machine

This application can be compiled and executed on my machine by entering the two commands shown in Figure 3 at the command prompt. (Note that artificial line breaks were inserted into Figure 3 to force the long commands to fit this narrow format.)

Figure 3 . Commands to compile and execute the application.
javac -cp .;M:\Baldwin\AA-School\Itse2321IntroOOP\ MediaCompBookMaterial\bookClasses Prob01.javajava -cp .;M:\Baldwin\AA-School\Itse2321IntroOOP\ MediaCompBookMaterial\bookClasses Prob01

The compiler and the virtual machine

The javac portion of the first command causes the Java compiler to run.

The java portion of the second command causes the Java virtual machine to run.

The input files

The Prob01.java and Prob01 at the ends of the two commands specify the files being operated on by the compiler and the virtual machine respectively.

A classpath

In both cases, the -cp indicates that a classpath follows.

A classpath consists of one or more path specifications separated by semicolon characters.

The purpose of the classpath is to tell the compiler and the virtual machine where to look for previously compiled class files that the application needs in order to successfully compile and execute.

The current folder

The period ahead of the semicolon says to search the current folder first.

The path to the Ericson library

The material following the semicolon is the absolute path to the folder containing the class files that make up Ericson's library on my machine. The location of that folder will probably be different on your machine.

The main method

Now that we have the preliminaries out of the way, let's go back and examine the body of the main method in Listing 1 .

The first statement in the body of the main method in Listing 1 instantiates a new object of the class named Prob01Runner .

The statement saves a reference to that object in a reference variable named obj . Note that the type of the variable is the same as the name of the class in this case. In general, the type of the variable must be:

  • The name of the class, or
  • The name of a superclass of the class, or
  • The name of an interface implemented by the class.

Accessing the object

You must save a reference to an object in order to gain access to the object later. In this case, the reference is stored in the variable named obj .

Call the method named run

The second statement in the body of the main method in Listing 1 uses that reference to call the method named run encapsulated in the object. As you will see later, most of the work in this application is performed in the method named run .

Get and display information about the object

When the run method returns control to the main method, the last three statements in the body of the main method in Listing 1 use the object's reference to call the following three methods belonging to the object:

<< Chapter < Page Page > Chapter >>

Read also:

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.
Jobilize.com uses cookies to ensure that you get the best experience. By continuing to use Jobilize.com web-site, you agree to the Terms of Use and Privacy Policy.