The be re-assigned a new value. A final

Topics: LifeChange


Sample donated:

Last updated: May 16, 2019

The final modifier keyword in java is usedto restrict the user. It doesn’t allow user to make changes in the value. Itcan only be assigned once and user can not modify the value once assigned. Final is a non-accessmodifier 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.FINALCLASS:A final class cannot have subclasses.

Don't use plagiarized sources.
Get Your Custom Essay on "The be re-assigned a new value. A final..."
For You For Only $13.90/page!

Get custom paper

We use final modifierwith the class to avoid the class from being sub-classed. It can’t be extended,nor can its features be inherited once final modifier is used.Examplepublic final class Test {   // body of class}This defines a class called Test which is publicly accessible but cannotbe sub-classed because of the final modifier.Classes with final modifier are to create an absolute class (likethe predefined String class).

A class cannot be made absolute withoutmaking it final.FINALVARIABLE:A final variableof primitive type is a constant, whose value cannot be changed. We cannot re-assignvalues and they must be remained constant throughout the program. Finalvariables are global constants. Final variablewith no value are called blank final variable or uninitialized final variable.It can be initialized in the constructor only.NamingConvention: All words are in uppercase separated by underscore ‘_’.

Examples: (MIN_WIDTH, MAX_VALUE, PI, RED)publicstatic final double PI = 3.141592653589793;publicstatic final int MAX_VALUE = 2147483647;MAX_ID =10000;   // error: cannot assign a value to finalvariable MAX_IDFINAL METHOD:A final method cannot be overridden by subclasses. A method cannotbe modified in a sub-class when final modifier is used.

Using final modifiers for methods increase efficiency allowing thecomplier to turn calls into methods. Methods like these are introduced so that anyoutsider can’t change the content of the method.Examples:public class Test {   public final void changeName() {      // body of method   }public final static randomNumber() {…  }}

Choose your subject


I'm Jessica!

Don't know how to start your paper? Worry no more! Get professional writing assistance from me.

Click here