Java programs can be written and executed in two ways:
Programming an "application" in Java is significantly different from programming an "applet." Applets are designed to be downloaded and executed on-line under control of a browser. Hence, their functionality is restricted in an attempt to prevent downloaded applets from damaging your computer or your data. No such restrictions apply to the functionality of a Java application.
All Java programs consist of one or more class definitions. In this course, we will refer to the primary class definition for a Java application as the controlling class.
A stand-alone Java application requires a method named main in its controlling class.
An Applet does not require a main method. The reason that
a Java Applet does not require a main method will be explained
in a subsequent lesson.
| HOW TO COMPILE AND RUN A JAVA APPLICATION
Here are the steps, based on the assumption that you are running under Win95. If you are running under Solaris, Mac, or some other operating system, you will have to translate these instructions to that OS. 1. Download and install the JDK from JavaSoft following the installation instructions at JavaSoft. Be sure to also download and install the documentation that is a separate download package (as of 9/22/98). 2. Using any editor that can produce a plain text file (such as Notepad), create a source code file with the extension on the file name being .java This file contains your actual java instructions. You can copy some samples out of the early lessons in this tutorial to get started. 3. Open an MSDOS box and change directory to the directory containing the source file. It doesn't really matter which directory the source file is in, but I normally put my java files in a directory all their own. 4. Let's assume that the name of the file is joe.java, just to have something definitive to refer to. 5. To compile the file, enter the following command at the prompt: javac joe.java 6. Correct any compiler errors that show up. Once you have corrected all compiler errors, the javac program will execute and return immediately to the prompt with no output. At that point, the directory should also contain a file named joe.class. This is the compiled file. 7. To run the program, enter the following command: java joe |
The main method in the controlling class of an application must be static, which results in main being a class method.
Class methods can be invoked without the requirement to instantiate an object of the class.
When a Java application is started, the Java interpreter finds and invokes the main method in the class whose name matches the name of the class file specified on the command line.
For example, to start the interpreter and run a Java application named
hello1,
a command-line statement such as the following must be executed at the
operating system prompt:
| java hello1 |
This statement instructs the operating system to start the java interpreter, and then instructs the interpreter to find and execute the java application stored in the file named hello1.class.
The following Java application displays "Hello World" on the screen.
/*File hello1.java
This is a Java application program .
When compiled, this program produces the class named:
hello1.class
When the Java interpreter is invoked upon the application's
controlling class using the following statement at the
command line:
java hello1
the interpreter starts the program by invoking the main
method defined in the controlling class. The main method is
a class method which can be invoked without the requirement
to instantiate an object of the class.
The program displays the following words on the screen:
Hello World
**********************************************************/
class hello1 { //define the controlling class
//define main method
public static void main(String[] args){
//display text string
System.out.println("Hello World");
}//end main
}//End hello1 class. No semicolon at end of Java class.
|
The features of classes and programs in Java will be discussed in detail in subsequent lessons. A brief discussion follows.
Java supports three styles of comments:
/** special documentation comment used by the javadoc tool */ /* Java/C/C++ style multi-line comment */ // Java/C/C++ style single-line comment |
The javadoc tool mentioned above is a program that is used to produce documentation for an application.
In Java, all functions (methods) and variables must be defined inside a class definition. There can be no freestanding functions or global variables.
Files containing source code in Java have an extension of java.
The controlling class definition for an application must contain the main method.
The file produced by the compiler containing the controlling class has the same name as the controlling class, and has an extension of class.
The java compiler produces a separate file for every class definition contained in an application or applet, even if two or more class definitions are contained in the same source file.
Thus, the compilation of a large application can produce many different class files. A capability known as a jar file can be used to consolidate those class files into a single file for more compact storage, distribution, and transmission.
The controlling class for a Java application must contain a static method named main. When you run the application using the Java interpreter, you specify the name of the class file that you want to run. The interpreter then invokes the main method defined in the class file having that name. This is possible because a class method can be invoked without a requirement to instantiate an object of the class.
The main method defined in that class definition controls
the flow of the program much as the main function in a C++ program
controls the flow of a stand-alone C++ program.
.
A - False. Applications are for stand-alone use while applets are for browsers.
Q - All applications and applets written in Java require a main method: True or False?
A - False. Applets do not require a main method while applications do require a main method.
Q - Explain the relationship between the name of the class file for a Java application and the location of the main method in the application.
A - The name of the class file must be the same as the name of the class which contains the main method (sometimes called the controlling class).
Q - Explain how you cause a method to be a class method in Java.
A - Preface the name of the method with the static keyword.
Q - Class methods can be invoked without the requirement to instantiate an object of the class: True or False?
A - True
Q - Write the source code for a Java application that will display your name and address on the standard output device. Show the command-line statement that would be required to execute a compiled version of your application.
A -
/*File Name01.java
This is a Java application that will display a
name on the standard output device.
The command required at the command line to execute this
program is:
java Name01
*/
class Name01 { //define the controlling class
public static void main(String[] args){ //define main method
System.out.println(
"Java\nProgramming\nat home, TX");
}//end main
}//End Name01 class.
Q - Show the three styles of comment indicators that are supported by Java.
A -
/** special documentation comment used by
the JDK javadoc tool */
/* C/C++ style multi-line comment */
// C/C++ style single-line comment
Q - Does Java allow free-standing functions or methods?
A - No, Java does not allow free-standing functions or methods.
Q - What is the relationship between the name of the controlling class in an application and the names of the files that comprise that application.
A - One of the files must have the same name as the controlling class with an extension of class.
Q - What is the relationship between the number of classes in an application and the number of separate files with the class extension that go to make up that application? How does this change when all the classes are defined in the same source file?
A - Each class definition results in a separate class file regardless of whether or not the classes are defined in separate source files.
Q - Class methods in Java can only be invoked relative to a specific object: True or False?
A - False. Class methods can be invoked by joining the name of the class with the name of the method using a period.
Q - Write the signature line for the main method in a Java application.
A - public static void main(String[] args)
Q - Write a Java application that meets the following specifications.
/*File SampProg02.java
Without reviewing the following solution, write an application
that will display your name on the screen.
================================================================
*/
class SampProg02 { //define the controlling class
public static void main(String[] args){ //define main method
System.out.println("Java programming");
}//end main
}//End SampProg02 class. Note no semicolon at end of Java class.
|