Java Reference
In-Depth Information
A preferable program would include a Java command that specifies how to draw
the box and then executes that command twice. Java doesn't have a “draw a box”
command, but you can create one. Such a named command is called a static method.
Static Method
A block of Java statements that is given a name.
Static methods are units of procedural decomposition. We typically break a class
into several static methods, each of which solves some piece of the overall problem.
For example, here is a static method to draw a box:
public static void drawBox() {
System.out.println("+------+");
System.out.println("| |");
System.out.println("| |");
System.out.println("+------+");
}
You have already seen a static method called main in earlier programs. Recall that
the main method has the following form:
public static void main(String[] args) {
<statement>;
<statement>;
...
<statement>;
}
The static methods you'll write have a similar structure:
public static void <name>() {
<statement>;
<statement>;
...
<statement>;
}
The first line is known as the method header. You don't yet need to fully under-
stand what each part of this header means in Java; for now, just remember that
you'll need to write public static void , followed by the name you wish to give
the method, followed by a set of parentheses. Briefly, here is what the words in the
header mean:
 
Search WWH ::




Custom Search