(^72) | Java Syntax and Semantics, Classes, and Objects
The application begins with a comment that explains what it does. Next comes an im-
portdeclaration that tells the compiler we will use input and output classes (for example,
BufferedReader) from the java.iopackage.
The importdeclaration is followed by the class heading, which begins with the modifier
public. Recall that we use publicto make the class accessible to the JVM. If you look a little
farther down in the application, you see that the heading of the mainmethod also begins with
public. The JVM must also be able to find mainto start execution there. In later chapters, we
will encounter situations where we want to limit access to a field, method, or class. We do
so by using the modifier private. If we used the privatemodifier with main, then the method
would be invisible to the JVM and it wouldn’t know where to start.
The class heading is followed by an open brace that begins the body of the class. The class
contains the method declaration for themainmethod. The first line of the method is its head-
ing.There are several points to emphasize regarding the method heading. Let’s take another look.
public static voidmain(String[] args) throwsIOException
We’ve already said that the publicmodifier is needed to make mainvisible to the JVM. The
keyword staticis another modifier that we will wait to explain more fully. The keyword void
identifies mainas a void method rather than a value-returning method. Of course,mainis
just the name of the method. Between the parentheses are the method’s parameters.
Parameters are declarations of special variables to which arguments are passed when the
method is called. That is, the values we place in the argument list of a method call are copied
to the parameter variables in the method.
The parameter to mainis an array of strings called args. (We explain arrays in Chapter 10.)
If we omitted the brackets following String, then the parameter would just be a string called
args. Adding the brackets tells Java that multiple strings may be passed to main. Who would
call main, and what values would they pass to it? When PrintNameis run as an application, the
operating system calls it. Some operating systems (such as Unix) allow the user to enter ar-
gument values that are passed to the application when it begins execution. In that case, the
application finds those values stored in the array of strings called args. We do not use such
arguments in this book, but Java still requires us to supply this specific parameter in the
heading of main. For now, you can just memorize this part of the heading and use it in your
applications.
At the end of the heading is a throwsclause. In many Java methods, when an unusual sit-
uation arises that cannot be handled within the method, the application indi-
cates the situation to the caller by throwing an exception. In Chapter 9, we will
explore how to throw exceptions and see how to use a catchstatement to respond
to an exception. For example, if readLinefinds too many characters on one line to
store into a string, it throws an IOException. Java requires that whenever a method
can throw an exception, we must either catch it or explicitly throw it to the next
level. For this reason,mainmust explicitly indicate, using a throwsclause, that it is
using a method that can throw an IOException. Then, if such an exception occurs,
the exception is passed up to the JVM and eventually the operating system out-
puts a message saying that the application terminated with an unusual condition.
Exception An unusual condi-
tion that is indicated by a
method using a throw
statement; the method’s caller
must either catch the exception
or explicitly throw it to the next
level
やまだぃちぅ
(やまだぃちぅ)
#1