CodesJava

Easy learning with example program codes

what is final parameter in java?


A method parameter declared with final keyword is known as final parameter. Its value can’t be changed.

Example

/**
 * This program is used to show that value of 
 * final parameter can't be changed.
 * @author CodesJava
 */
class Test{
	public void showDouble(final int num){
		//error because value of final parameter can't be changed.
		num = num * 2;
		System.out.println("Num * 2 = " + num);
	}
}
public class FinalExample7 {
	public static void main(String args[]){
		//creating object of Test class
		Test obj = new Test();
		//method call
		obj.showDouble(10);
	}
}

Output

Exception in thread "main" java.lang.Error: 
Unresolved compilation problem:
The final local variable num cannot be assigned. 
It must be blank and not using a compound assignment
at com.codesjava.business.Test.showDouble
(FinalExample7.java:11)
at com.codesjava.business.FinalExample7.main
(FinalExample7.java:20)

Java interview questions on final keyword



Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap