Java Reference
In-Depth Information
to contain them. PrintExample has nothing to do with JFrame s or Date s or any
other API class that we have seen; rather, it exists only as an organizational tool.
We do not want to customize an existing class, so we leave off the extends
clause. We place the method inside the class as before:
public class PrintExample {
/** Print b , c , and b+c on separate lines */
public static void print3 ( int b, int c) {
System.out.println(b);
System.out.println(c);
System.out.println(b+c);
}
}
How do we call print3 with, say, arguments -3 and 4 ? Much like we write
Math.max(-3, 4) to call static method max in class Math . We write:
PrintExample.print3(-3, 4);
Before you continue, type class PrintExample into your IDE and have a call to
print3 executed.
Variable scope
The scope of a variable is the area of a program in which that variable can
be used. The scope of a parameter is the method body. Thus, two different meth-
public class PrintExample {
/** Print b , c , and b+c on separate lines */
public static void print3 ( int b, int c) {
System.out.println(b);
System.out.println(c);
System.out.println(b+c);
}
/** Print b+c */
public static void printSum( int b, int c) {
System.out.println(b + c);
}
/** Print b+c and b+c*c */
public static void printSums( int b, int c) {
printSum(b, c);
printSum(b, c * c);
}
}
Figure 2.1:
Class PrintExample, with three procedures
Search WWH ::




Custom Search