0% found this document useful (0 votes)
3 views11 pages

01 Modifiers

Uploaded by

Valentino Krauss
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views11 pages

01 Modifiers

Uploaded by

Valentino Krauss
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Advanced Programming

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)

Value must be assigned either inline or in the constructor

class Person {
String name;
final int birthYear;

Person(String name, int birthYear) {


this.name = name;
this.birthYear = birthYear; After this line of code, the value
of birthYear can’t change
}
}
Variables are final
Are the methods on the right side allowed? class Example {
final int[] fibonacci;

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)

class Person { There is a single (static)


static final int MIN_ADULT_AGE = 18; minimum adult age that is
shared among every person
String name;
final int birthYear; Each person has its own
(non-static) birth year
Person(String name, int birthYear) {
this.name = name;
this.birthYear = birthYear;
}
}
Access modifiers
static and final are non-access modifiers
(change the behavior of methods or variables)

The other group of modifiers are called access modifiers


(change visibility of classes, methods and variables)

By default, these things have package local visibility


Packages
Also called namespaces or modules

Represented by folders in the file system of the computer

Used for two reasons:


● Organizing classes that belong together
(similar to how folders are used to organize files that belong together)
● Avoid naming conflicts
(multiple classes can have the same name, as long as they are in different packages)

If a class relies on a class that’s in a different package,


an import statement is necessary.
Usually the IDE does this for the programmer.
Package local
Default visibility level of classes, methods and variables in Java

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.

Live example! Code will be posted on Canvas after the lecture.


Public
Increases visibility level of classes, methods and variables in Java

public keyword

If a class, method, or variable has the public visibility, it can be seen from everywhere (when imported).

Live example! Code will be posted on Canvas after the lecture.


Private
Reduces visibility level of classes, methods and variables in Java

private keyword

If a class, method, or variable has the private visibility, it can only be seen within that same file.

Live example! Code will be posted on Canvas after the lecture.

You might also like