Things to Know Part 4
Methods:
-required parts:
return type, name, parenthesis (for arguments), and body (if not abstract)
-optional parts:
access modifier (public, protected, or private), specifier (final, static, abstract, synchronized*, native*, strctftp*), exception (throws Exception) *not tested
-return types other than void must return something of that type. be careful of return statements in if-statements and loops that may not get executed.
Variable Arguments:
void someMethod(int… arguments){} //<-- takes in 0 or more ints. must be last argument
//of list and can only have one per method
Access Modifiers:
public: can be called from any class
protected: can be called from classes in same package or from any subclass
private: can only be called within the same class. main method can access private members of the class that it is in.
-defaults to package private. This means only classes in the same package can call it.
Method Specifiers:
-static
creates only one copy of method in memory. can be called with class name and without object.
-abstract
abstract classes and interfaces only. body of method is not defined. subclasses and implementing classes must define method.
-final
method cannot be overwritten by a subclass
Static Imports:
-imports static members (variables and methods) from a class.
import static java.util.Arrays.binarySearch;//<--imports the static binarySearch()
//method from the Arrays class
-static code blocks get run first but only once
public class MyClass {
{System.out.println("Test 1");}
static {System.out.println("Test 2");}
MyClass(){System.out.println("Test 3");}
}
//when the first MyClass object is created, it prints:
//Test 2
//Test 1
//Test 3
//when every other MyClass object is created, it prints:
//Test 1
//Test 3
Constructors:
-every class has default constructor only if no constructor is provided by user.
-can be overloaded as long as parameters are different.
-this() can be used in one constructor to call an overridden constructor of the same class. can also have parameters.
Encapsulation:
-encapsulate member variables by making them private and creating getters and setters / accessors and mutators.
-immutable classes have encapsulated data
Lambdas:
-can be used to pass a method function as a parameter.
-used wherever an object of an interface that only has one abstract method can be used.
public interface Lambda {
void method();
}
public interface NotLambda {
void methodOne();
void methodTwo();
}
-syntax without parameters
someMethod(() -> {
if(10 > 0){
System.out.println("OKAY");
}
});
//if lambda definition has only one expression, curly braces and semicolon may
//be omitted
someMethod(() -> System.out.println("OKAY"));
-syntax with parameters
someMethod((Obj a) -> {a.do();});
someMethod((a) -> {a.do();});
someMethod(a -> {a.do();});
someMethod((Obj a, Obj b) - > {a.do(); b.do();});
someMethod((a, b) -> {a.do(); b.do();});
source:
Boyarsky, Jeanne, and Scott Selikoff. OCA Oracle Certified Associate. Study Guide. John Wiley & Sons, 2015.