1. "Hello, World!"
  2. Variables and Types
  3. Arrays
  4. While, If, For
  5. ...Problem Set 0
  6. Static Methods
  7. Static Fields
  8. String Conversion
  9. Objects
  10. Threading
  11. Strings
  12. ...Problem Set 1.5
  13. Packages
  14. Complex Numbers
  15. Abstract classes
  16. Interfaces
  17. Autoboxing
  18. ...Problem Set 1
  19. enum
  20. Inner Classes
  21. Polymorphism
  22. Tanks!
  23. Callbacks
  24. Exceptions
  25. File I/O
  26. ...Problem Set 2
  27. Regular Expressions

Static Methods

In this next example we solve the quadratic equation three times. It's a shame we have to re-type that whole equation, isn't it?

Meth1.java
1public class Meth1 {
2    public static void main(String[] args) {
3        double a = 1c = 2b = 4;
4        double root1 = -(b + Math.sqrt(b*b-4.0*a*c))/(2.0*a);
5        double root2 = -(b - Math.sqrt(b*b-4.0*a*c))/(2.0*a);
6        System.out.println(a+","+b+","+c+" => "+root1+","+root2);
7        double a2 = 1.5c2 = 1.8b2 = 4.01;
8        double root1p = -(b2 + Math.sqrt(b2*b2-4.0*a2*c2))/(2.0*a2);
9        double root2p = -(b2 - Math.sqrt(b2*b2-4.0*a2*c2))/(2.0*a2);
10        System.out.println(a2+","+b2+","+c2+" => "+root1p+","+root2p);
11        double a3 = 0.8c3 = 1.9b3 = 3.95;
12        double root1n = -(b3 + Math.sqrt(b3*b3-4.0*a3*c3))/(2.0*a3);
13        double root2n = -(b3 - Math.sqrt(b3*b3-4.0*a3*c3))/(2.0*a3);
14        System.out.println(a3+","+b3+","+c3+" => "+root1n+","+root2n);
15    }
16}
$ javac Meth1.java
$ java Meth1
1.0,4.0,2.0 => -3.414213562373095,-0.5857864376269049
1.5,4.01,1.8 => -2.102615606073059,-0.570717727260274
0.8,3.95,1.9 => -4.397409265526184,-0.540090734473816

The good news is that we don't. We can re-write this program using static methods to encapsulate and re-use the quadratic equation code. This also makes it easier to read.

Meth2.java
1public class Meth2 {
2    static void get_and_print_roots(double a,double b,double c) {
3        double root1 = (-b - Math.sqrt(b*b-4.0*a*c))/(2.0*a);
4        double root2 = (-b + Math.sqrt(b*b-4.0*a*c))/(2.0*a);
5        System.out.println(a+","+b+","+c+" => "+root1+","+root2);
6    }
7    public static void main(String[] args) {
8        get_and_print_roots(1,4,2);
9        get_and_print_roots(1.5,4.01,1.8);
10        get_and_print_roots(.8,3.95,1.9);
11    }
12}
$ javac Meth2.java
$ java Meth2
1.0,4.0,2.0 => -3.414213562373095,-0.5857864376269049
1.5,4.01,1.8 => -2.102615606073059,-0.570717727260274
0.8,3.95,1.9 => -4.397409265526184,-0.540090734473816

By moving the code to calculate and print the roots to a static method named get_and_print_roots() we can reduce the size of our code. It is now smaller, and simpler to understand.

Meth3.java
1public class Meth3 {
2    static double get_root1(double a,double b,double c) {
3        return (-b - Math.sqrt(b*b-4.0*a*c))/(2.0*a);
4    }
5    static double get_root2(double a,double b,double c) {
6        return (-b + Math.sqrt(b*b-4.0*a*c))/(2.0*a);
7    }
8    static void get_and_print_roots(double a,double b,double c) {
9        double root1 = get_root1(a,b,c);
10        double root2 = get_root2(a,b,c);
11        System.out.println(a+","+b+","+c+" => "+root1+","+root2);
12    }
13    public static void main(String[] args) {
14        get_and_print_roots(1,4,2);
15        get_and_print_roots(1.5,4.01,1.8);
16        get_and_print_roots(.8,3.95,1.9);
17    }
18}
$ javac Meth3.java
$ java Meth3
1.0,4.0,2.0 => -3.414213562373095,-0.5857864376269049
1.5,4.01,1.8 => -2.102615606073059,-0.570717727260274
0.8,3.95,1.9 => -4.397409265526184,-0.540090734473816

Here we've broken up the code a bit more. By doing this we have made the calculation of each root it's own method, allowing us to re-use this bit of code in contexts where it is not immediately printed out.

Sometimes methods compute values, rather than printing things to the screen, and we need a mechanism to identify that value. The "return" statement is what does it. In get_root() we use the "return" statement to pass back the result of a mathematical expression.

Notice that the argument to return is a double, and that matches the type name used in front of get_root1(). Both the methods get_and_print_roots() as well as main() are of type "void" -- that is to say that they do not compute and return any value.

Overload.java
1public class Overload {
2    public static void what(double d) {
3        System.out.println("It's a double");
4    }
5    public static void what(String s) {
6        System.out.println("It's a string");
7    }
8    public static void what(int i) {
9        System.out.println("It's an integer");
10    }
11    public static void main(String[] args) {
12        what(3);
13        what("hi");
14        what(2.4);
15    }
16}
$ javac Overload.java
$ java Overload
It's an integer
It's a string
It's a double

This short program illustrates a concept called "overloading." Even though there are three methods with the name "what()", Java is smart enough to know which one you want to call based on the argument you pass to it.

Recursion.java
1public class Recursion {
2    static void count(int n,int last) {
3        if(n >= last) return;
4        System.out.println("n="+n);
5        count(1+n,last);
6    }
7    public static void main(String[] args) {
8        count(2,5);
9    }
10}
$ javac Recursion.java
$ java Recursion
n=2
n=3
n=4

The count method in the above program is recursive -- that is it calls itself. Recursion can be a very useful technique in programming. Generally, however, any program that could be written with recursion can be re-written as a loop. You have to decide which is a better choice for your particular problem.