Final local variables

18 January 2012 By Nithya Vasudevan 169 views No Comment
0 Flares Twitter 0 Facebook 0 Google+ 0 0 Flares ×
  • 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 
		}
	}
}