What Is The Final Keyword In Java?

ExploreEXPLORE THE CATALOGSupercharge your career with 700+ hands-on coursesView All CoursesPythonJavaJavaScriptCReactDockerVue JSRWeb DevDevOpsAWSC#LEARNING TOOLSExplore the industry's most complete learning platformCoursesLevel up your skillsSkill PathsAchieve learning goalsProjectsBuild real-world applicationsMock InterviewsNewAI-Powered interviewsPersonalized PathsGet the right resources for your goalsLEARN TO CODECheck out our beginner friendly courses.PricingFor BusinessResourcesNewsletterCurated insights on AI, Cloud & System DesignBlogFor developers, By developersFree CheatsheetsDownload handy guides for tech topicsAnswersTrusted answers to developer questionsGamesSharpen your skills with daily challengesSearchCoursesLog InJoin for freeWhat is the final keyword in Java?

In Java, the final keyword can be used while declaring an entity. Using the final keyword means that the value can’t be modified in the future. This entity can be - but is not limited to - a variable, a class or a method.

%0 node_1 final node_2 variables node_1->node_2 node_1560346245217 parameters node_1->node_1560346245217 node_1560346202102 methods node_1->node_1560346202102 node_3 classes node_1->node_3

Final variables

If a variable is declared with the final keyword, its value cannot be changed once initialized. Note that the variable does not necessarily have to be initialized at the time of declaration. If it’s declared but not yet initialized, it’s called a blank final variable.

Example:

// declaring a final variableclass FinalVariable { final int var = 50; var = 60 //This line would give an error}

For a final reference variable you cannot change what object it refers to. You can, however, modify the object itself.

Example:

class Reference{ public int value = 5;}class frVariable { public static void FinalReference( String args[] ) { final Reference example = new Reference(); //declaration example.value = 6; // Modifying the object creates no disturbance Reference another = new Reference(); example = another; // Attempting to change the object it refers to, creates an error }}Run

Final parameters

If you ever see the final keyword with a parameter variable, it means that the value of this variable cannot be changed anywhere in the function.

Example:

class finalParameter { public static void example( final int parameter ) { parameter = 4; //attempting to reassign a value to a parameter throws an error }}Run

Final methods

A method, declared with the final keyword, cannot​ be overridden or hidden by subclasses.

Example:

// declaring a final methodclass Base{ public final void finalMethod(){ System.out.print("Base"); }}class Derived extends Base{ public final void finalMethod() { //Overriding the final method throws an error System.out.print("Derived"); }}Run

Final classes

A class​ declared as a final class, cannot be subclassed

Example:

// declaring a final classfinal class FinalClass { //...}class Subclass extends FinalClass{ //attempting to subclass a final class throws an error //...}Run

Relevant Answers

Explore Courses

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved

Tag » What Is Finally Keyword In Java