What is a final parameter in java?

Java 8Object Oriented ProgrammingProgramming

You can pass final variables as the parameters to methods in Java.

A final variable can be explicitly initialized only once. A reference variable declared final can never be reassigned to refer to a different object. However, the data within the object can be changed. So, the state of the object can be changed but not the reference. With variables, the final modifier often is used with static to make the constant a class variable.

Example

Live Demo

public class Test{
   public void sample(final int data){
      System.out.println(data);
   }
   public static void main(String args[]) throws Exception{
      Test t = new Test();
      t.sample(500);
   }
}

Output

500
raja
Published on 10-Jan-2018 10:59:47
Advertisements