class ClassName {The word "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
...
}
class ClassName extends OtherClassName {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.
...
}
private type variable-name ;and always appears inside the curly-braces of a class definition, but not inside any of its method definitions. For example,
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;
public return-type method-name ( parameter-variable declaration, ... parameter-variable declaration ) {The word "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.
...
}
public int doSomething (int myParam) {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.
int localVar;
localVar = myParam * myParam - 3;
return localVar;
}
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.
abstract public return-type method-name ( parameters );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" before the word "class" in the class header.
public class-name ( parameters ) {Note that there is no return-type, and in place of a method-name is the name of the class, spelled exactly the same way as in the class header. For example,
local-variable declaration
local-variable declaration
...
statement
statement
...
}
class Foo {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.
public Foo (int x) {
myX = x;
}
private int myX;
}
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.
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)