Site Loader
Rock Street, San Francisco

The final modifier keyword in java is used to restrict the user. It doesn’t allow userto make changes in the value. It can only be assigned once and user can not modifythe value once assigned.Final is a non-access modifier valid only to a variable, a method or a class.? A final class cannot be sub-classed (or extended).? A final variable cannot be re-assigned a new value.? A final method cannot be overridden in the subclass.FINAL CLASS:A final class cannot have subclasses. We use final modifier with the class to avoid theclass from being sub-classed. It can’t be extended, nor can its features be inheritedonce final modifier is used.Example:public final class Test {// body of class }This defines a class called Test which is accessible everywhere but cannot be subclassedbecause of the final modifier.Classes with final modifier are to create an absolute class (like the predefined Stringclass). A class cannot be made absolute without making it final.FINAL VARIABLE:A final variable of primitive type is a constant, whose value cannot be changed. Wecannot re-assign values and they must be remained constant throughout theprogram. Final variables are global constants.Final variable with no value are called blank final variable or uninitialized finalvariable. It can be initialized in the constructor only.Naming Convention: All words are in uppercase separated by underscore ‘_’.Example: (MIN_WIDTH, MAX_VALUE, PI, RED)public static final double PI = 3.141592653589793;public static final int MAX_VALUE = 2147483647;MAX_ID = 10000; // error: cannot assign a value to final variable MAX_IDFINAL METHOD:A final method cannot be overridden by subclasses. A method cannot be modified ina sub-class when final modifier is used.Using final modifiers for methods increase efficiency allowing the complier to turncalls into methods. Methods like these are introduced so that any outsider can’tchange the content of the method.Example:public class Test {public final void changeName() {// body of method}public final static randomNumber() {…}}

Post Author: admin


I'm Dora!

Would you like to get a custom essay? How about receiving a customized one?

Check it out