java final modifier

The java final modifier can be attached to all classes, methods and variables, Each has a different meaning.

When attaching to a class [Prohibit inheritance]

If you add the final modifier to a class, that class cannot be inherited. An error will occur at the compilation stage.

//final modifier class
final class FinalDemo {
    String name = "Class with final";
    public final void getInfo() {
        System.out.println(this.name);
    }
}

//Small class
class FinalDemoChild extends FinalDemo {

}

class MyApp {
    public static void main(String[] args) {
        FinalDemoChild demo = new FinalDemoChild();
    }
}

Execution result

$ java Myapp
Error:(8, 21) java: final final_Cannot be inherited from demo

When attaching to a method [Prohibit override]

If you add final to a method of a class, you cannot override it in the method child class.

//Class with final
class FinalDemo {
    String name = "Class with final";
    public final void getInfo() {
        System.out.println(this.name);
    }
}

//Small class
class FinalDemoChild extends FinalDemo {
    @Override
    public void getInfo() {
        System.out.println(this.name + "Small class");
    }
}

class MyApp {
    public static void main(String[] args) {
        FinalDemoChild demo = new FinalDemoChild();
    }
}

Execution result

$ java MyApp
Error:(11, 17) java:FinalDemoChild getInfo()Is the Final Demo getInfo()Cannot be overridden
Overridden method is final

When attaching to a variable [Prohibition of reassignment (constant)]

If you add final to a variable (field), you cannot reassign it. That is a constant. Since it is not necessary to change it with instance, it seems that it is often declared with a class variable with static. Also, as in other languages, constants are written in all capital letters.

//Class with final
class FinalDemo {
    public static final String NAME = "Variable with final";
}


class MyApp {
    public static void main(String[] args) {
        //Change constant
        FinalDemo.NAME = "Reassignment";
    }
}

Execution result

$ java MyApp 
Error:(9, 18) java:You cannot assign a value to the final variable NAME

Recommended Posts

java final modifier
final modifier
Access modifier [Java]
JAVA learning history final modifier and static modifier
Java abstract modifier [Note]
Java tips --StaticUtility class modifier
Java basic learning content 5 (modifier)
Java
Java
Java learning (0)
Studying Java ―― 3
[Java] array
[Java Silver] Summary of access modifier points
Java protected
[Java] Annotation
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Java method
java (constructor)
Java array
Difference between final and Immutable in Java
java (override)
java (method)
Java Day 2018
Java string
Java marks PPT document as final state
Java static
Java serialization
java beginner 4
JAVA paid
Studying Java ―― 4
Java (set)
[Java] compareTo
Studying Java -5
java reflexes
java (interface)
Java memorandum
☾ Java / Collection
Java array
[Java] Array
Studying Java # 0
Java review
java framework
Java features
[Java] Inheritance
FastScanner Java
Java features
java beginner 3
Java memo
Java inheritance
[Java] Overload
Java basics
[Java] Annotation
java beginner
Java (add2)
JAVA (Map)
Java9 collection