Java Program Structure

A Java program consists of one or more classes. For standalone applications, you must have a class with a public void static main method. Applets require only a subclass of the Applet class.

The Java 2 SDK expects the source files to have the same name as the class, but with a .java extension (thus, a class named Circle is defined in the file Circle.java). Within each source file, the parts of the program are laid out in the following manner:

1. The file starts with some comments that describe the purpose of the class and provide other pertinent information such as the name of author and revision dates. Java supports both C and C++-style comments. As in ANSI C, comments may start with a /* and end after a */ pair. Or, you may simply begin a line of comment with a pair of slashes (//). A special type of comment, known as doc comment, begins with a /** and ends with */. The doc comments can be extracted by the javadoc utility program to create online documentation for classes.

2. One or more import statements that allow you to use abbreviated class names. For example, if you use the java.applet.Applet class, you can refer to that class by the short name Applet provided you include the following import statement in the source file:

import java.applet.*;

Note that the import statement does not really bring in any extra code into a class; it's simply a way to save typing so you can refer to a class by a shorter name (for example, Applet instead of java.applet.Applet).

3. The class definition that includes instance variables and methods. All variables and methods must be inside the class definition.

Was this article helpful?

0 0
Make Money Writing

Make Money Writing

This Report Will Show You How To Make Money By Providing Writing Services To Other Internet Marketers. Learn how to make money by writing the right way. Grab your copy of this report now and learn. Why writing is a great way to earn money. How to compete with cheap writers, even if you charge a lot more money.

Get My Free Ebook


Post a comment