Writing Static Methods

  1. void methods with no parameters

    public class MyCode { public static void doSomething()1 { System.out.println("Carpe diem"); } public static void main(String[] args) { doSomething(); doSomething(); } }
  2. void methods with one parameter
    public class MyCode { public static void printSquare(int x) { System.out.println(x + "^2 = " + (x*x)); } public static void main(String[] args) { printSquare(2); printSquare(3); } }
  3. void methods with multiple parameters
    public class MyCode { public static void printSum(int x, int y) { System.out.println(x + " + " + y + " = " + (x+y)); } public static void main(String[] args) { printSum(2,3); printSum(3,4); } }
  4. int methods (Methods that return a value)
    public class MyCode { public static int square(int n) { return n*n; } public static void main(String[] args) { System.out.println("3^2 = " + square(3)); System.out.println("4^2 = " + square(4)); } }
  5. Methods calling other methods
    public class MyCode { public static int square(int n) { return n*n; } public static void printSquare(int x) { System.out.println(x + "^2 = " + square(x)); } public static void main(String[] args) { printSquare(3); printSquare(4); } }
  6. Other return types (double, boolean, char, String)
    public class MyCode { public static double cubeRoot(double d) { return Math.pow(d, 1.0/3.0); } public static boolean isPositive(int x) { return (x > 0); } public static char firstChar(String s) { return s.charAt(0); } public static String initials(String firstName, String lastName) { return "" + firstName.charAt(0) + lastName.charAt(0); } public static void main(String[] args) { System.out.println(cubeRoot(8)); System.out.println(isPositive(8)); System.out.println(isPositive(-8)); System.out.println(firstChar("ABCD")); System.out.println(initials("Douglas","Adams")); System.out.println(firstChar(initials("Douglas","Adams"))); } }
  7. Local variables
    public class MyCode { public static String initials(String firstName, String lastName) { char firstInitial = firstName.charAt(0); char lastInitial = lastName.charAt(0); return "" + firstInitial + lastInitial; } public static void main(String[] args) { System.out.println(initials("Donald","Knuth")); } }

    Another example:

    public class MyCode { public static boolean isEvenPositive(int x) { boolean isEven = ((x % 2) == 0); boolean isPositive = (x > 0); return (isEven && isPositive); } public static void main(String[] args) { System.out.println(isEvenPositive(-2)); System.out.println(isEvenPositive(-1)); System.out.println(isEvenPositive(0)); System.out.println(isEvenPositive(1)); System.out.println(isEvenPositive(2)); } }
    Yet another example:

    public class MyCode { public static int minSquared(int x, int y) { int min = Math.min(x, y); return (min * min); } public static void main(String[] args) { System.out.println(minSquared(3, 4)); System.out.println(minSquared(4, 3)); } }
  8. Parameter and Local variable scope

    public class MyCode { public static int addOne(int x) { x = x + 1; System.out.println("in addOne, x = " + x); return x; } public static void main(String[] args) { int x = 5; System.out.println("in main, x = " + x); System.out.println(addOne(x)); System.out.println("in main, x = " + x); } }



    Another example:

    public class MyCode { public static void printN() { System.out.println(n); // ERROR: n is not defined here! } public static void main(String[] args) { int n = 5; printN(); } }

    Yet another example:

    public class MyCode { public static void main(String[] args) { int n = 5; System.out.println(n); int n = 10; // ERROR: n is already defined! System.out.println(n); } }
  9. Test methods

    public class MyCode { public static int minSquared(int x, int y) { int min = Math.min(x, y); return (min * min); } public static void testMinSquared() { System.out.print("Testing minSquared... "); assert(minSquared(2,3) == 4); assert(minSquared(3,2) == 4); System.out.println("Passed all tests!"); } public static void main(String[] args) { // remember to enable assertions (with -ea flag) testMinSquared(); } }


    Another example:

    public class MyCode { public static boolean isOdd(int x) { return ((x % 2) == 1); // ERROR: This contains a bug! } public static void testIsOdd() { System.out.print("Testing isOdd... "); assert(!isOdd(2)); assert(isOdd(1)); assert(!isOdd(0)); assert(isOdd(-1)); // ERROR: This assertion will fail! assert(!isOdd(-2)); System.out.println("Passed all tests!"); } public static void main(String[] args) { testIsOdd(); } }