The final keyword in Java

  1. Final variable.
  2. Final method.
  3. Final class.

Final variable

DOES NOT COMPILE
  • Once a value is assigned we cannot change it.
  • We use the final variable as constants in our code.
  • static final is a common approach to create constant values in Java.
DOES NOT COMPILE
  • final keyword can be used with local variables as well. The idea is the same cannot change the value.
DOES NOT COMPILE
  • can be used with method arguments. Again cannot reassign the value of an argument.

Final method

Main2.java DOES NOT COMPILE
  • In the above example, we tried to override the final method and we are getting a compilation error.

Final class

Main2.java DOES NOT COMPILE

--

--

--

Software Developer, Java Instructor https://www.techleadacademy.io/

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Front End Developer: Complete RoadMap

Max point from cards — Daily Challenge May

Managing Engineers, tools from raising a Toddler

First Steps in IoT

Superstore Analysis Using MySQL

Demystifying Error Handling in Rust

Automated testing AI applications ..

Optimizing Brain Power: Preamble

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Beknazar

Beknazar

Software Developer, Java Instructor https://www.techleadacademy.io/

More from Medium

Pyramid Pattern of Numbers code in java

Java Programs

Java Encapsulation

Constructors in Java