Final local variables

  • All local variables can only be marked with final non-access modifier with default access modifier. Trying to use any other access or non-access modifier will result in a compiler error.
package com.ibytecode.keywords.finaldemo;

public class FinalLocalVariable {
	public static void main(String[] args) {
		final int localVarOne = test();
		final int localVarTwo = 15;
		// localVarOne++; //ERROR
		int sum = localVarOne + localVarTwo;
		System.out.println("Sum = " + sum);
	}

	public static int test() {
		return 12;
	}

	public static void test1(int j, int k) {
		final int c = j + k;
		//c++; //ERROR
		for (int i = 0; i < 10; i++) {
			final int value = i;
			//++value; //ERROR 
		}
	}
}

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.