final keyword in java.
1 min readJul 5, 2023
this post will help users understand about final keyword in the java programming language.
- final keyword is used to restrict users .
- final keyword can be used with variables, methods and classes and method parameters.
- final variables are usually used as constant in java, as their value can not be changes .
- final variables can only be assigned once and we can use constructor to assign their values .
- if we try to change the final variable it gives us the compile time error.
Get Ranuj Mahajan’s stories in your inbox
Join Medium for free to get updates from this writer.
// 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;
}
}






