4 : Language Fundamentals
Identify correctly constructed
source files, package declarations, import statements,
class declarations (of all forms including inner
classes), interface declarations and implementations
(for java.lang.Runnable or other interface described
in the test), method declarations (including the main
method that is used to start execution of a class),
variable declarations and identifiers.
A source file should generally contain
at most one top level public class definition. If a
public class is present, the class name should match
the unextended file name. A source file may contain an
unlimited number of non public class definitions.
This is not a language requirement, but
an implementation requirement of many compilers
including the reference compilers from sun. It is
therefore unwise to ignore this convention since doing
so limits the portability of your source files ( but
not, of course, your object files).
There are 3 top level elements that may
or may not appear in a file. If they are present then
the order must be
and comments may appear before or after any of these
Keywords and reserved words may not be
used as identifiers. An identifier must begin with a
letter, a dollar sign ($), an underscore ( _ ) or
digits. You can use embedded keywords.
This method is declared
public by convention. However it is a requirement that
it be static so that it
may be executed without the necessity of constructing
an instance of the corresponding class.
return type must be void. These conditions are
necessary because when a Java program starts running,
JVM looks for this method as the code to start
execution of the program is placed in this method
Variables and Initialization
variables have two different lifetimes :
A member variable of a
class is created when the instance is created and is
accessible from any method in the class.
A local variable of a
method is created on entry to the method, exists
only during execution of the method, and is
accessible only within the method.
variable may be initialized in its own declaration
line : when this technique is used, non-static instance variables are
initialized just before the class constructor is
executed. Static variables are initialized at class
variables are not initialized by the system – every
such variable must be explicitly initialized before
section4-1 | section4-2 | section4-3 | section4-4
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11