Java supports three styles of comments. The one shown at the top of the program is called
a multiline comment. This type of comment must begin with /* and end with */. Anything
between these two comment symbols is ignored by the compiler. As the name suggests, a
multiline comment may be several lines long.
The next line of code in the program is shown here:
class Example {
This line uses the keyword class to declare that a new class is being defined. Example is an
identifier that is the name of the class. The entire class definition, including all of its members,
will be between the opening curly brace ({) and the closing curly brace (}). For the moment,
don't worry too much about the details of a class except to note that in Java, all program
activity occurs within one. This is one reason why all Java programs are (at least a little bit)
object-oriented.
The next line in the program is the single-line comment, shown here:
// Your program begins with a call to main().
This is the second type of comment supported by Java. A single-line comment begins with
a // and ends at the end of the line. As a general rule, programmers use multiline comments
for longer remarks and single-line comments for brief, line-by-line descriptions. The third
type of comment, a documentation comment, will be discussed in the "Comments" section later
in this chapter.
The next line of code is shown here:
public static void main(String args[]) {
This line begins the main( ) method. As the comment preceding it suggests, this is the line
at which the program will begin executing. All Java applications begin execution by calling
main( ). The full meaning of each part of this line cannot be given now, since it
involves a detailed understanding of Java's approach to encapsulation. However, since
most of the examples in the first part of this topic will use this line of code, let's take a brief
look at each part now.
The public keyword is an access specifier, which allows the programmer to control the
visibility of class members. When a class member is preceded by public, then that member
may be accessed by code outside the class in which it is declared. (The opposite of public
is private, which prevents a member from being used by code defined outside of its class.)
In this case, main( ) must be declared as public, since it must be called by code outside of
its class when the program is started. The keyword static allows main( ) to be called without
having to instantiate a particular instance of the class. This is necessary since main( ) is
called by the Java Virtual Machine before any objects are made. The keyword void simply
tells the compiler that main( ) does not return a value. As you will see, methods may also
return values. If all this seems a bit confusing, don't worry. All of these concepts will be
discussed in detail in subsequent chapters.
As stated, main( ) is the method called when a Java application begins. Keep in mind that
Java is case-sensitive. Thus, Main is different from main. It is important to understand that
the Java compiler will compile classes that do not contain a main( ) method. But java has no
way to run these classes. So, if you had typed Main instead of main, the compiler would
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home