Final parameter in java

In java, final parameter is a parameter which is declared with final keyword. Its value can not be changed. Example

class FinalParameterTest{
 
	void show(final String website){
		System.out.println("website = " + website);
	}
}
public class Main {
	public static void main(String args[]){
		//creating object of FinalParameterTest Class
		FinalParameterTest obj = new FinalParameterTest();
		obj.show("w3spoint.com");
	}
}

Output

website = w3spoint.com

we will get compile time error, if we try to change the final parameter. Example

class FinalParameterTest{
 
	void show(final String website){
	    website = "java.com";
		System.out.println("website = " + website);
	}
}
public class Main {
	public static void main(String args[]){
		//creating object of FinalParameterTest Class
		FinalParameterTest obj = new FinalParameterTest();
		obj.show("w3spoint.com");
	}
}

Output

Main.java:4: error: final parameter website may not be assigned
	    website = "java.com";
	    ^
1 error

Java interview questions on final keyword

Please follow and like us:
Content Protection by DMCA.com