01 Modifiers
01 Modifiers
Modifiers
Modifiers
public static final int PI = 3.14;
Reserved words
Change (modify) the behavior and visibility of classes, methods and variables
Two types:
● Access modifiers
● Non-access modifiers
Final
A final variable can only be assigned a value once
(assigned ➞ using the assignment operator)
class Person {
String name;
final int birthYear;
Example() {
fibonacci =
new int[] {0, 1, 1, 2, 3, 8};
}
void a() {
fibonacci[5] = 5;
}
void b() {
fibonacci =
new int[] {0, 1, 1, 2, 3, 5};
}
}
Variables are final
Are the methods on the right side allowed? class Example {
final int[] fibonacci;
Method a: yes
The array variable fibonacci itself is final, but the Example() {
elements inside of the array are not. Therefore we can fibonacci =
change one of those elements. new int[] {0, 1, 1, 2, 3, 8};
}
Method b: no
The variable fibonacci is final, so we can’t assign a
void a() {
new value to it.
fibonacci[5] = 5;
}
void b() {
fibonacci =
new int[] {0, 1, 1, 2, 3, 5};
}
}
Static
Variable becomes property of the class instead of a property of the instance
Making a variable static essentially means that the variable exists only once (since the class exists once)
No keyword
If a class, method, or variable has the package local visibility, it can only be seen by other classes in that
same package.
public keyword
If a class, method, or variable has the public visibility, it can be seen from everywhere (when imported).
private keyword
If a class, method, or variable has the private visibility, it can only be seen within that same file.