Menu

1 / 2

Final Keyword in java

DEV Community·Harini·6 months ago
#RqVyP9ij
#java#beginners#basic#software#final
Reading 0:00
15s threshold

In Java, the final keyword is used to restrict changes. Once something is marked as final, it cannot be modified further. It can be applied in three main places: variables, methods, and classes.

1. Final Variable

A final variable acts like a constant — its value cannot be changed once assigned.

public class GRT { final int price = 15000 ;

public void display (){ GRT grt = new GRT (); grt . price = 200 ;

System . out . println ( grt . price ); } }

Enter fullscreen mode

Exit fullscreen mode

Key point:

You must assign value once

After that, no reassignment allowed

2. Final Method

A final method cannot be overridden by subclasses.

class Parent { final void show () { System . out . println ( "This is final method" ); } }

class Child extends Parent { // void show() { } ❌ Error: cannot override final method }

Enter fullscreen mode

Exit fullscreen mode

Key point:

Used when you want to prevent method overriding

3.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.