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

Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki