Method declaration
public final void nap(int minutes) throws InterruptedException {
// take a nap
}Two of the parts—the method name and parameter list—are called the method signature.
ACCESS MODIFIERS
> [!NOTE:]
There’s a default keyword in Java. You saw it in the switch statement and interfaces.
It’s not used for access control.
Valid?
public void walk1() {}
default void walk2() {} // DOES NOT COMPILE
void public walk3() {} // DOES NOT COMPILE
void walk4() {}OPTIONAL SPECIFIERS
static modifier is used for class methods and will be covered later in this chapter.abstract modifier is used when a method body is not provided. It will be covered in Chapter 9.final modifier is used when a method is not allowed to be overridden by a subclass. It will also be covered in Chapter 8.synchronized modifier is used with multithreaded code. It is on the 1Z0-816 exam, but not the 1Z0-815 exam.native modifier is used when interacting with code written in another language such as C++. It is not on either OCP 11 exam.strictfp modifier is used for making floating-point calculations portable. It is not on either OCP 11 exam.public void walk1() {}
public final void walk2() {}
public static final void walk3() {}
public final static void walk4() {}
public modifier void walk5() {} // DOES NOT COMPILE
public void final walk6() {} // DOES NOT COMPILE
final public void walk7() {}> [!NOTE:]
Remember that a method must have a return type.
If no value is returned, the return type is void.
You cannot omit the return type.
public void walk1() {}
public void walk2() { return; }
public String walk3() { return ""; }
public String walk4() {} // DOES NOT COMPILE
public walk5() {} // DOES NOT COMPILE
public String int walk6() { } // DOES NOT COMPILE
String walk7(int a) { if (a == 4) return ""; } // DOES NOT COMPILEint integer() {
return 9;
}
int longMethod() {
return 9L; // DOES NOT COMPILE
}
int integerExpanded() {
int temp = 9;
return temp;
}
int longExpanded() {
int temp = 9L; // DOES NOT COMPILE
return temp;
}can’t return a long primitive in a method that returns an int.
public void walk1() {}
public void 2walk() {} // DOES NOT COMPILE
public walk3 void() {} // DOES NOT COMPILE
public void Walk_$() {}
public _() {} // DOES NOT COMPILE
public void() {} // DOES NOT COMPILE_ method is not allowed since it consists of a single underscore.public void walk1() {}
public void walk2 {} // DOES NOT COMPILE
public void walk3(int a) {}
public void walk4(int a; int b) {} // DOES NOT COMPILE
public void walk5(int a, int b) {}public void zeroExceptions() {}
public void oneException() throws IllegalArgumentException {}
public void twoExceptions() throws IllegalArgumentException, InterruptedException {}public void walk1() {}
public void walk2() // DOES NOT COMPILE
public void walk3(int a) { int name = 5; }The walk2() method doesn’t compile because it is missing the braces around the empty method body.
Working with Varargs
public void walk1(int... nums) {}
public void walk2(int start, int... nums) {}
public void walk3(int... nums, int start) {} // DOES NOT COMPILE
public void walk4(int... start, int... nums) {} // DOES NOT COMPILEThe walk3() and walk4() methods do not compile because they have a varargs parameter in a position that is not the last one.
Can you figure out why each method call outputs what it does?
15: public static void walk(int start, int... nums) {
16: System.out.println(nums.length);
17: }
18: public static void main(String[] args) {
19: walk(1); // 0
20: walk(1, 2); // 1
21: walk(1, 2, 3); // 2
22: walk(1, new int[] {4, 5}); // 2
23: }walk(1, null); // throws a NullPointerException in walk()
16: public static void run(int... nums) {
17: System.out.println(nums[1]);
18: }
19: public static void main(String[] args) {
20: run(11, 22); // 22
21: }Applying Access Modifiers
> [!NOTE:]
The Java module system redefines “anywhere,” and it becomes possible to restrict access to public code. When given a code sample, you can assume it isn’t in a module unless explicitly stated otherwise.
> [!NOTE:]
Remember to look at the reference type for a variable when you see a static method or variable. The exam creators will try to trick you into thinking a NullPointerException is thrown because the variable happens to be null. Don’t be fooled!
A static method or instance method can call a static method because static methods don’t require an object to use. Only an instance method can call another instance method on
the same class without using a reference variable, because instance methods do require an object. Similar logic applies for the instance and static variables.
STATIC INITIALIZATION
instance initializers that looked like unnamed methods—just code inside braces. Static initializers look similar. They add the static keyword to specify they should be run when the class is first loaded.
All static initializers run when the class is first used in the order they are defined. The statements in them run and assign any static variables as needed.
14: private static int one;
15: private static final int two;
16: private static final int three = 3;
17: private static final int four; // DOES NOT COMPILE
18: static {
19: one = 1;
20: two = 2;
21: three = 3; // DOES NOT COMPILE
22: two = 4; // DOES NOT COMPILE
23: }TRY TO AVOID STATIC AND INSTANCE INITIALIZERS
Using static and instance initializers can make your code much harder to read. Everything that could be done in an instance initializer could be done in a constructor instead. Many people find the constructor approach is easier to read.
There is a common case to use a static initializer: when you need to initialize a static field and the code to do so requires more than one line. This often occurs when you want to initialize a collection like an ArrayList. When you do need to use a static initializer, put all the static initialization in the same block. That way, the order is obvious.