-
Notifications
You must be signed in to change notification settings - Fork 0
Method
Keyhan Hadjari edited this page Sep 16, 2016
·
7 revisions
Describes the access level of a method. The list below is ordered from lowest to highest restriction level.
- public can be accessed from any class.
- protected can be accessed from within same class, sub classes and classes within same package.
- Default Also called Package private, can be accessed within same class or classes in same package.
- private can be accessed from within same class.
- abstract used in abstract classes, method has to be implemented in non-abstract subclasses.
- final cannot be overriden in subclasses.
- native The method points to code written in native languages like C/C++.
- static Class method or variable, can be accessed without initialization.
- strictfp Makes floating point calculations portable.
- synchronized thread safe method by locking it up during usage
The java type that is returned from the method, it could be a simple type like int or a Class object like String.
- Has to be made of letters, numbers, $ or _ signs.
- Cannot start wit number
- Cannot be a reserved word like public.
- 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)
- Is optional
- starts with keyword throws
- comma seperated to create list
- _throws NumberFormatException, NullpointerException
- Is surrounded by {}
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;
}- 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 field and method can be called even for null objects, they behave the same.
- Static member cannot call instance member. IMPORTANT
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();
}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 COMPILEpublic 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
}