The final
modifier is used to mark a class final so that it cannot inherited, to prevent a method being overridden, and to prevent changing the value of a variable. Arguments of a method if declared as final
is also can not be modified within the method.
package org.kodejava.example.fundamental;
public class FinalExample {
// breed is declared final.
// can't change the value assigned to breed
public final String breed = "pig";
private int count = 0;
// sound() method is declared final, so it can't be overridden
public final void sound() {
System.out.println("oink oink");
}
// number parameter is declared final. can't change the value
// assigned to number
public int count(final int number) {
// assign a value to number variable will cause a
// compile-time error
//
// number = 1;
count = +number;
return count;
}
public static void main(String[] args) {
FinalExample fe = new FinalExample();
// assign a value to breed variable will cause a
// compile-time error
//
// fe.breed = "dog";
int number = fe.count(20);
}
}
final class SubFinalExample extends FinalExample {
// try to override sound() method of superclass will cause a
// compile-time error
//
// public void sound() {
// System.out.println("oink");
// }
}
// try to inherit a class that declared final will cause a
// compile-time error
//
class OtherFinalExample extends SubFinalExample {
}
Latest posts by Wayan (see all)
- How do I install Calibri font in Ubuntu? - January 24, 2021
- How do I create a generic class in Java? - January 1, 2021
- How do I convert java.util.TimeZone to java.time.ZoneId? - April 25, 2020