Sitemap

final keyword in java.

1 min readJul 5, 2023

--

this post will help users understand about final keyword in the java programming language.

    Get Ranuj Mahajan’s stories in your inbox

    Join Medium for free to get updates from this writer.

  1. final keyword is used to restrict users .
  2. final keyword can be used with variables, methods and classes and method parameters.
  3. final variables are usually used as constant in java, as their value can not be changes .
  4. final variables can only be assigned once and we can use constructor to assign their values .
  5. if we try to change the final variable it gives us the compile time error.
// Online Java Compiler
// Use this editor to write, compile and run your Java code online

class HelloWorld {
public static void main(String[] args) {
FinalTest finalTest= new FinalTest("test");
System.out.println(finalTest.name);
}
}
class FinalTest{
public final String name;
public String lname;

FinalTest(String name){
this.name=name;
}

public void setName(String name){
System.out.println("name cann't be set here");
}

public void setLName(final String name){
this.lname=name;
}
}

--

--

Ranuj Mahajan
Ranuj Mahajan

Written by Ranuj Mahajan

I am curious mind. i am very simple human being in this complicated consiousness. i try to explain concepts of computers and life in simple and precise manner.