OCA Java SE 8 Method - Java Method Signature








Method Name

Method names follow the same rules as variable names.

Method names may only contain letters, numbers, $, or _.

The first character of method name cannot be a number, and reserved words are not allowed.

By convention, methods begin with a lowercase letter but are not required to.

myMethod1() is a valid method declaration with a traditional name.

public void myMethod1() { } 

2myMethod() doesn't compile because identifiers are not allowed to begin with numbers.

public void 2myMethod() { } // DOES NOT COMPILE 

myMethod3() doesn't compile because the method name is before the return type.

public myMethod3 void() { } // DOES NOT COMPILE 

MyMethod_$() is a valid method declaration.

public void MyMethod_$() { } 

The final line of code doesn't compile because the method name is missing.

public void() { } // DOES NOT COMPILE 




Parameter List

The parameter list can be empty, such as void myMethod(){}.

For multiple parameters, you separate them with a comma.

myMethod1() is a valid method declaration without any parameters.

public void myMethod1() { } 

myMethod2() doesn't compile because it is missing the parentheses around the parameter list.

public void myMethod2 { } // DOES NOT COMPILE 

myMethod3() is a valid method declaration with one parameter.

public void myMethod3(int a) { } 

myMethod4() doesn't compile because the parameters are separated by a semicolon rather than a comma. Semicolons separates statements, not parameter lists.

public void myMethod4(int a; int b) { }  // DOES NOT COMPILE 

myMethod5() is a valid method declaration with two parameters.

public void myMethod5(int a, int b) { } 




Varargs

A method may use a vararg parameter (variable argument) as if it is an array.

A vararg parameter must be the last element in a method's parameter list.

This implies you are only allowed to have one vararg parameter per method.

myMethod1() is a valid method declaration with one vararg parameter.

public void myMethod1(int... nums) { } 

myMethod2() is a valid method declaration with one int parameter and one vararg parameter.

public void myMethod2(int start, int... nums) { } 

myMethod3() and myMethod4() do not compile because they have a vararg parameter in a position that is not the last one.

public void myMethod3(int... nums, int start) { } // DOES NOT COMPILE 
public void myMethod4(int... start, int... nums) { } // DOES NOT COMPILE 

When calling a method with a vararg parameter, you can pass in an array, or you can list the elements of the array.

You can even omit the vararg values in the method call and Java will create an array of length zero.

public class Main{
   public static void main(String[] argv){
         myMethod(1);                    // 0 
         myMethod(1, 2);                 // 1 
         myMethod(1, 2, 3);                 // 2 
         myMethod(1, new int[] {4, 5});     // 2 
   
   }
    public static void myMethod(int start, int... nums) { 
     System.out.println(nums.length); 
    } 
}

Java will create an empty array if no parameters are passed for a vararg. Passing null explicitly will throws a NullPointerException.

myMethod(1, null);     // throws a NullPointerException 

Since null isn't an int, Java treats it as a null array reference.

Java passes on the null array object to myMethod. Then the myMethod() method throws an exception because it tries to determine the length of null.

Accessing a vararg parameter is like accessing an array. It uses array indexing.

For example:

public static void run(int... nums) { 
  System.out.println(nums[1]); 
} 
public static void main(String[] args) { 
  run(11, 22);     // 22 
} 

Optional Exception List

You can add exception type in method signature.

In the example, InterruptedException is a type of Exception. You can list as many types of exceptions as you want in this clause separated by commas.

For example:

public void zeroExceptions() { } 
public void oneException() throws IllegalArgumentException { } 
public void twoExceptions() throws  
  IllegalArgumentException, InterruptedException { } 

Method Body

The final part of a method declaration is the method body.

A method body is a code block with braces that contain zero or more Java statements.

myMethod1() is a valid method declaration without an empty method body.

public void myMethod1() { } 

myMethod2() doesn't compile because it is missing the braces around the empty method body.

public void myMethod2; // DOES NOT COMPILE 

myMethod3() is a valid method declaration with one statement in the method body.

public void myMethod3(int a) { int name = 5; }