Chapter 4: Methods and Encapsulation Flashcards
What parts of a method are required for the code to compile?
Return type Method name Parameter list (can be empty ()) Method body (can be empty {})
What are the access modifiers in order from most to least accessible?
public
protected
[default]
private
Can a package private class be accessed from a child class in a different package?
No. Package private is default accessibility and can only be accessed within the same package.
What does final mean when applied to a method?
The method cannot be overridden
What is the syntax for a method’s exception list?
Keyword throws followed by a comma separated list of exceptions
throws Exception1, Exception2, …
What is the correct syntax for varargs?
datatype… name
What are the rules for using varargs parameter?
It must be the last parameter and there can be only one vararg in a given parameter list
How do you pass data to a method with a vararg?
Data can be passed in as comma separated values or as an array
How do you access data from a vararg within a method?
Treat it like an array
When can you call methods on a null reference variable?
When the method is static
Can a static member call an instance member?
No
What runs first, static initializer or intance initializer?
Static
When is the last time a value can be assigned to a final instance variable?
In the constructor
When do static initializer blocks run?
When the class is first used
What do static imports do?
Allow importing of static class members