Chapter 7 Methods and Encapsulation Notes Flashcards
Method declaration
public final void nap(int minutes) throws InterruptedException { // take a nap }
- access modifier (optional)
- optional specifier (optional)
- return type (required)
- method name (required)
- parentheses (required)
- list of parameters (can be empty)
- exception (optional)
- method body (can be emtpy, body omitted for abstract methods)
Two of the parts—the method name and parameter list—are called the method signature.
ACCESS MODIFIERS
- The private modifier means the method can be called only from within the same class.
- Default (Package-Private) Access With default access, the method can be called only from classes in the same package. This one is tricky because there is no keyword for default access. You simply omit the access modifier.
- The protected modifier means the method can be called only from classes in the same package or subclasses. You’ll learn about subclasses in Chapter 8, “Class Design.”
- The public modifier means the method can be called from any class.
> [!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() {}
- The walk2() method doesn’t compile because default is not a valid access modifier.
- The walk3() method doesn’t compile because the access modifier is specified after the return type.
OPTIONAL SPECIFIERS
- The
static
modifier is used for class methods and will be covered later in this chapter. - The
abstract
modifier is used when a method body is not provided. It will be covered in Chapter 9. - The
final
modifier is used when a method is not allowed to be overridden by a subclass. It will also be covered in Chapter 8. - The
synchronized
modifier is used with multithreaded code. It is on the 1Z0-816 exam, but not the 1Z0-815 exam. - The
native
modifier is used when interacting with code written in another language such as C++. It is not on either OCP 11 exam. - The
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() {}
- The walk5() method doesn’t compile because modifier is not a valid optional specifier.
- The walk6() method doesn’t compile because the optional specifier is after the return type.
- The walk7() method does compile. Java allows the optional specifiers to appear before the access modifier.
> [!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 COMPILE
- The walk4() method doesn’t compile because the return statement is missing.
- The walk5() method doesn’t compile because the return type is missing.
- The walk6() method doesn’t compile because it attempts to use two return types. You get only one return type.
- The walk7() method is a little tricky. There is a return statement, but it doesn’t always get run. If a is 6, the return statement doesn’t get executed. Since the String always needs to be returned, the compiler complains.
int 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
- The 2walk() method doesn’t compile because identifiers are not allowed to begin with numbers.
- The walk3() method doesn’t compile because the method name is before the return type.
- The Walk_$() method is a valid declaration. While it certainly isn’t good practice to start a method name with a capital letter and end with punctuation, it is legal.
- The
_
method is not allowed since it consists of a single underscore. - The final line of code doesn’t compile because the method name is missing.
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) {}
- The walk2() method doesn’t compile because it is missing the parentheses around the parameter list.
- The walk4() method doesn’t compile because the parameters are separated by a semicolon rather than a comma. Semicolons are for separating statements, not for parameter lists.
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
- a method may use a varargs parameter (variable argument) as if it is an array.
- A varargs parameter must be the last element in a method’s parameter list. This means you are allowed to have only one varargs parameter per method.
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 COMPILE
The 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()
- Line 19 passes 1 as start but nothing else. This means Java creates an **array of length 0 for nums. **
- Line 20 passes **1 as start **and one more value. Java converts this one value to **an array of length 1. **
- Line 21 passes 1 as start and two more values. Java converts these two values to an array of length 2.
- Line 22 passes 1 as start and an array of length 2 directly as nums.
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
- private: Only accessible within the same class
- Default (package-private) access: private plus other classes in the same package
- protected: Default access plus child classes
- public: protected plus classes in the other packages
> [!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!