Skip to main content

Posts

Showing posts with the label final keyword in java

Final Modifier

Final Modifier:- Final is a modifier in java and this modifier is applicable for variable, method and class. Final modifier is used for restricting to change. Using this modifier we can declare final variable, final method and final class. If variable is declared as a final then cannot change the value or cannot reassign the value of final variable. If method is declared as a final then that method cannot redefine by the child classes or child classes cannot override final methods. If class is declared as a final then that class cannot inherit. Example1:-final variable (Customer.java) class Customer { final int accNo;   } Output:- C:\JAVATECH>javac Customer.java Customer.java:3: error: variable accNo not initialized in the default constructor final int accNo;           ^ 1 error Example2:- class Customer { final int accNo; Customer() { accNo=101; } } Output:-  ...