typewriter
font
are Java keywords, and must be spelled exactly as I do.
They may be in any order.
They may be in separate files; indeed, BlueJ, DrJava, and some
other Java platforms automatically put each class
definition in a separate file (e.g. the definition of
class Employee
would be in file Employee.java
).
If they're not all in separate files, each one must end before the next one starts. (This isn't actually a rule of the Java language, so if you break it the compiler won't tell you this is what you've done wrong; it'll just give you a lot of other error messages. In fact, in a month or two you'll want to do this intentionally, but not yet.)
In an Application, exactly one of the classes must be
public
, you must somehow tell the Java platform it's the
"main class" (how you do this varies from CodeWarrior
to BlueJ to DrJava to Symantec Cafe to...), and it must contain a
public static void main
method. See below for more
on this.
In an Applet, one of the classes must "extend" the predefined Applet class. More on this later.
A class header usually looks like
The word "class
ClassName {
...
}
public
" may be before the word
"class
". As mentioned above, you should probably
do this only for the "main class". In place of the "..."
(between the curly-braces), you'll usually put one or more instance variable declarations and one or
more method definitions. These may be in
any order, but to avoid confusion it's best to pick a convention and
stick to it. I usually use the order
instance variable declarations, if any,
constructor method definitions, if any,
method definitions for access
methods (which typically have names like getX()
or
setY(int yArg)
),
method definitions for "standard"
methods like equals()
and toString()
, if any,
method definitions for "interesting" methods, i.e. the ones specific to this class, and finally
a method definition for a
static method named test
,
which tries a fixed set of test cases for this class.
public
things (instance variables, constructors, and
regular methods) first, then all the protected
things,
then all the private
things. For the duration of this
class, I would prefer that you use my convention; when you get a
job programming in Java, use whatever convention is used in that
company. A class header may also look like
This indicates that ClassName inherits the properties of OtherClassName (which should already be defined somewhere), but adds or overrides some of the methods and instance variables.class
ClassNameextends
OtherClassName {
...
}
and always appears inside the curly-braces of a class definition, but not inside any of its method definitions. For example,private
type variable-name;
class Foo {
private int x;
...
}
The words "public
" or "protected
"
may be substituted for "private
", but not unless
you have a good reason to do so ("the program doesn't compile"
does not count as a good reason in itself!) In most cases,
if other classes really need to get at your instance variables, I
recommend writing access methods instead of making the
variables themselves public
.
Instance variables can be initialized, e.g.
private int x=3;
although I tend to favor initializing instance variables explicitly
inside a constructor.
You can also define several variables on the same line, e.g.
private int x=3, y=7, z;
A method header most often looks like
The word "public
return-type method-name(
parameter-variable declaration, ... parameter-variable declaration) {
...
}
private
" should be substituted for
"public
" if the method is used only by
other methods in the same class. The return-type should be
"void
" if the method is not intended to return
any useful information to its caller. For example,
public int doSomething (int myParam) {
int localVar;
localVar = myParam * myParam - 3;
return localVar;
}
Note that the parameter variables declared in the header (in this
example, myParam
) can be used in the body. You don't need
to specify their types again, because Java remembers what types you
said they were before. Note also that these parameter variables
do not exist outside the curly-braces of the method; if you
try to use them anywhere else, you'll get a syntax error. The keyword "static
"
may be inserted before
the return-type to indicate that the method can be called
even if there isn't an existing object of the class to call it on.
This allows other code to call your method more easily, but places
restrictions on what you can do inside the method: in
particular, since the method wasn't called "on" any
particular object, there is no "this
" variable,
and so you cannot call other non-static
methods in the class
without specifying what object they apply to.
For example, if you had a subclass of DrawingTool
with the
static
method
public static void turnAround () { turnClockwise(); this.turnClockwise(); }you would get a compiler error message. First, when you write "
turnClockwise();
" without specifying an object,
Java figures you mean "this.left();
" (in other words,
the two turnClockwise()
statements mean exactly the same
thing). But this
doesn't
make sense inside a static
method because there is no
"this
object". To look at it another way, the
turnAround
method written above has a DrawingTool "tell
itself" to turn left twice, but the word "itself"
doesn't mean anything inside a static
method because no
particular DrawingTool was told to turnAround
.
The same method would make perfectly good sense if it weren't
static
:
public void turnAround () { turnClockwise(); this.turnClockwise(); }Alternatively, it could be a method in another class that refers explicitly to a specific
DrawingTool
:
public class Director { private DrawingTool pencil; public Director () { this.pencil = new DrawingTool (); ... } public void turnAround () { pencil.turnClockwise(); this.pencil.turnClockwise(); }Again,
turnClockwise()
is always being called on a
specific object.
A method definition may alternatively look like
with no body at all, not even curly-braces. This defines an abstract method, a method which is not fully defined in this class but only in subclasses of this one. If you have any abstract methods in your class, the class itself should be declared abstract by putting the word "abstract public
return-type method-name(
parameters);
abstract
" before
the word "class
" in the class header. A
"constructor" is a
special kind of method, called automatically whenever anybody
creates a new
instance of the class. Its definition
looks like
Note that there is only one word between thepublic
class-name(
parameters) {
local-variable declaration
local-variable declaration
...
statement
statement
...
}
public
and the parentheses, and that word must be
the name of the class, spelled exactly
the same way as in the class header. For example,
class Foo {
private int myX;
public Foo (int x) {
this.myX = x;
}
}
defines a class named Foo
with an int
instance
variable named myX
and a constructor which takes a
parameter named x
and "memorizes" it by copying
it into the instance variable myX
. In fact, about 90% of
all constructors look like this: they do nothing but copy their
parameters into instance variables.
An incorrect version of the above would be
class Foo {
private int myX;
public void Foo (int x) {
this.myX = x;
}
}
This won't produce any compiler errors, but it's not a
constructor because the void
between
public
and Foo
makes Java think it's an
ordinary method named Foo
that doesn't return
anything. Technically, it's perfectly legal in Java for a method
name to be the same as the name of the class it's in. But it's
confusing: such a method looks almost like a constructor,
but can't be invoked using new Foo(5)
syntax as a
constructor would. Don't do this.
Since a constructor has no return type, it should not end
with a "return
" statement. The usual purpose of
a constructor is to initialize all the instance variables,
so that when other methods for the class are called, they can assume
that the instance variables have meaningful values.
Under some circumstances, a constructor may be private
or
protected
rather than public
. We'll discuss
this later.
private
, public
, or
protected
, and it is always inside a method.
It looks like
type-name varname, varname, ... varname;Note that you can declare several local variables of the same type in one declaration, or one in each of several statements; it doesn't matter. Like instance variables, local variables can be initialized:
type-name varname=value, varname=value;
An example of a method header with several parameter variable declarations follows:
public int myMethod (int thisOne, int thatOne, String theOtherOne)The following examples are incorrect and will produce syntax errors:
public int myMethod (int thisOne, thatOne, String theOtherOne)(because
thatOne
has no type specified)
public int myMethod (int thisOne=4, int thatOne, String theOtherOne)(because
thisOne
is initialized)
public int myMethod (int thisOne, int thatOne, String theOtherOne;)(because semicolons aren't allowed inside the parentheses of a method header)