CHP5 - Methods Flashcards
method declaration rule
-First comes access modifier.
Access modifiers and optional specifiers can be listed in any order, but once the return type is specified, the rest of the parts of the method are written in a specific order: name, parameter list, exception list, body.
protected access modifier
Members with the protected modifier are accessible:
- Within the same package (like default access).
- In subclasses, even if they are in a different package.
Useful when designing classes for inheritance.
Default (Package-Private) Access Modifier
-If no access modifier is specified, the member is package-private.
-Members are accessible only within the same package.
-Cannot be accessed from subclasses if they are in a different package.
Variable naming rules
An identifier may only contain letters, numbers, currency symbols, or _ .
Also, first character is not allowed to be a number, and reserved words are not allowed.
Single underscore character is not allowed.
Method signature
It is composed of the method name and parameter list.
Local variable modifier
There is only one modifier that can be applied to local variable.
It is final modifier.
We can use var and final together.
Does using final modifier mean we cant modify the data?
Nope.
The final attribute refers only to the variable reference;
final description
Specifies that the instance variable must be initialized with each instance of the class exactly once
volatile description
Instructs the JVM that the value in this variable may be modified by other threads
transient
Used to indicate that an instance variable should not be serialized with the class
null as varargs
Triggers NullPointerException. Becuase it tries to determine the length of null
static method call
You can use instance of the object to call a static method.
The compiler checks for the type of the reference and uses that instead of the object.
static methods can be called with even null objects.
Autoboxing
Process of converting a primitive into its equivalent wrapper class
Unboxing
Process of converting a wrapper class into its equivalent primitive
Implicit Casting (Widening)
– Automatic and safe conversion (small → large type).
byte → short, int, long, float, double
short → int, long, float, double
char → int, long, float, double
int → long, float, double
long → float, double
float → double
Explicit Casting (Narrowing)
– Manual conversion (large → small type) with potential data loss.
double → float, long, int, short, byte
float → long, int, short, byte
long → int, short, byte
int → short, byte, char
short → byte
Is {true, true} proper array declaration?
NO.
new boolean[] {true, true} is proper declaration
Are Rope.swing() and new Rope().swing() same?
Yes if swing() method is public static method
static final initialization period
If a variable is static final, it must be set exactly once, and it must be in the declaration line or in a static initialization block.
static variable access from initializers
Both static and instance initializers are able to acess static variables.
clone() method
a clone() creates shallow copy which means elements of original and duplicate point to the same objects