Skip to content
Keyhan Hadjari edited this page Sep 16, 2016 · 7 revisions

Method anatomy

Access modifier

Describes the access level of a method. The list below is ordered from lowest to highest restriction level.

  1. public can be accessed from any class.
  2. protected can be accessed from within same class, sub classes and classes within same package.
  3. Default Also called Package private, can be accessed within same class or classes in same package.
  4. private can be accessed from within same class.

Optional Modifier

  1. abstract used in abstract classes, method has to be implemented in non-abstract subclasses.
  2. final cannot be overriden in subclasses.
  3. native The method points to code written in native languages like C/C++.
  4. static Class method or variable, can be accessed without initialization.
  5. strictfp Makes floating point calculations portable.
  6. synchronized thread safe method by locking it up during usage

Return Type

The java type that is returned from the method, it could be a simple type like int or a Class object like String.

Method name

  • Has to be made of letters, numbers, $ or _ signs.
  • Cannot start wit number
  • Cannot be a reserved word like public.

Parameters

  • Parameter list is mandatory
  • It can be empty
  • Order is first type of the param and then name of the param
  • comma seperate params to create list
    • (String name, int age)

Exception List

  • Is optional
  • starts with keyword throws
  • comma seperated to create list
    • _throws NumberFormatException, NullpointerException

Body

  • Is surrounded by {}

Example

Method example

// Access modifier = public
// Optional Modifier = static
// Return type = int
// method name = findMax
// parameters = x and y
// exceptions = FormatException
public static int findMax(String x, String y) throws FormatException {
    int result;
    // Some code
    return result;
}

Varargs

  • Variable argument (vararg) is an array of arguments in parameter list.
  • Must be the last parameter in parameter list.
private void printNames(String lastName, String... firstNames) {
    for (String firstName : firstNames) {
        System.out.println(firstName + " " + lastName);
    }
}

public void testFamily() {
    printNames("Kennedy","John", "Jack", "Robert"); //1
    printNames("Kennedy",new String[]{"John", "Jack", "Robert"}); //2
    printNames("Kennedy", null); //3 NullpointerException
}
// 1 and 2 Results in prnting lines below.
// John Kennedy
// Jack Kennedy
// Robert Kennedy

Static methods and fields.

  • Static field and method can be called even for null objects, they behave the same.
  • Static member cannot call instance member. IMPORTANT

pass by value

In java the data is passed by value and not reference. It means that when calling a method the value of the parameters are copied.

.
.
int a = 1;
changeVal(a);
System.out.println(a); // still 1
String b = "Adam";
changeStr(b);
System.out.println(b); // still Adam
StringBuilder c = "Adam";
changeStrBuilder(c);
System.out.println(c); // Changed to madA
.
.

void changeVal(int a) {
    a = 10;
}

void changeStr(String b) {
    b = "Bert";
}

void changeStrBuilder(StringBuilder c) {
    c.reverse();
}

Overloading

public void fly(int n){}
public int fly(int n){} // DOES NOT COMPILE
public static void fly(int n){} // DOES NOT COMPILE
public void fly(short n){} //OK

public void fly(int[] l){}
public void fly(int... l){} // DOES NOT COMPILE

Autoboxing

public void foo(int i, int j) {} // 1st choice
public void foo(long i, long j) {} // 2nd choice
public void foo(Integer i, Integer j) {} // 3rd choice
public void foo(int... nums) {} // 4th choice
public void foo(Long i, Long j) {} // NO CHOICE, Can not handle 2 steps, only change to long and not Long.

public static void main(String[] args) {
    foo(12, 13); // Look above for matches
}

Clone this wiki locally