Site Search:

Create methods with arguments and return values; including overloaded methods

Back OCAJP


We have came across many methods so far, for example, the famous main method looks like the following:
public static void main(String...args) {System.out.println("hello");}
  • public is the access modifier, other modifiers are protected, default (no modifier) and private. 
  • static is an optional specifier, other optional specifiers are abstract, final, synchronized (non OCA), native (non OCA) and strictfp (non OCA). 
  • void is the return type, return type could be void (means nothing is returned), primitive type, or object. 
  • main is the method name, method name is an java identifier, it can only contains alphabetics, $ and _, the first character can not be a number, reserved word is not allowed.
  • String...args are method parameter list. Parameter list is a comma separated method parameters, the parameter list can have zero or multiple parameters. String...args defines an Varagrgs, you can pass in an array or a list of strings or nothing at all as method parameters. An vararg parameter must be the last element in a method's parameter list.
  • Optionally, a method can have exception list, the caller of the method have to catch the exceptions or throw them.
  • {} is the method body, it is not optional.

OCAJP>cat test.java 
class test{
  public static void main(String... args) {
    try{
      System.out.println(getString("a", 2, "b","...c"));
    } catch(Exception o) {}
  }
  private static final synchronized String getString(String a, int b, String...bs) throws NullPointerException,IllegalArgumentException {
    String tmp = a + b;
    for(String i: bs)
      tmp+=i;
    return tmp; 
  }
}
OCAJP>javac test.java 
OCAJP>java test
a2b...c


For OCAJP test, we have to be aware of what method declarations don't work, and quickly select the do not compile option.

OCAJP>cat test.java 
class test{
  public static void main(String... args) {
     test t = new test();
     t.right(1, new int[] {0}, new int[] {2});
     //t.right(1, {0}, 1, 2, 3);  //not compile
     t.right(1, new int[] {}, new int[]{1,2});  //pass in an array
     t.right(1, new int[] {}, 1, 2);  //pass in some number of int parameters
     t.right(1, new int[] {});  //pass in an empty array
     t.right(1, null, null);  //pass in null
     //t.right(1, 2, 3, 4);  //error: incompatible types: int cannot be converted to int[]
  }
  //private void m1 {}  //error: '(' expected
  public void method() {return;}
  //public void method1() {return null;}  //error: incompatible types: unexpected return value
  //public int method2() {return 1L;}  //error: incompatible types: possible lossy conversion from long to int
  //String m(){}  //error: missing return statement
  //default access modifier is ommit the access modifier, default is not a keyword
  static final synchronized void m() {}
  //default String m2() {}  //error: modifier default not allowed here
  //optional specifiers can not appear between return type and method name
  //public void final m3() {}  //error: <identifier> expected
  void $_235() {}
  //String met-8() {return "";}  //- is not allowed
  int certain(String a) { if(a=="something") return 1; else return 0;}
  //int uncertain(String a) { if(a=="something") return 1;}  //error: missing return statement
  //int wrong() {return 3L;}  //error: incompatible types: possible lossy conversion from long to int
  //public void wrong();  //error: missing method body, or declare abstract
  public void right(int a, int[] b, int...args) {}
  public void right1(int a, int[] b, int[] c, String[] d, int...args) {}  //ok to have arrays multiple arrays
  //public void wrong(int a, int... b, int...args) {}  //varargs have to be the last one in parameter list, only one is allowed
}
OCAJP>javac test.java 
OCAJP>java test


Methods in the same class can have the same name but different type parameters, when this happen, we call it method overloading. 






OCAJP>cat test.java 
class test{
  public static void main(String... args) {
    test t = new test();
    t.m1();
    t.m1(1);
    System.out.println(t.m1("xxx"));
    t.m1(1,2,3,4,5,6,7,8);
    t.m1(1, new int[]{});
  }
  void m1() {}
  //String m1() {}  //error: method m1() is already defined in class test
  void m1(int a){}
  public final String m1(String c) {return c;}
  void m1(int a, int b, int...c){};
  //void m1(int a, int b, int[] c){};  //error: cannot declare both m1(int,int,int[]) and m1(int,int,int...) in test
  void m1(int a, int[] b){};
}
OCAJP>javac test.java 
OCAJP>java test
xxx

Back OCAJP