[BUG] Missing error? blank final may not have been initialized

Alex Buckley alex.buckley at oracle.com
Mon Apr 7 22:09:20 UTC 2014


I don't believe javac has ever given an error in this case. It would be 
unreasonable to require 'data' to be definitely assigned before the body 
of the anonymous class. OTOH, names in the body of a lambda expression 
are specified as if they appear outside the lambda expression, where 
'data' would be definitely unassigned, so an error is due there.

Alex

On 4/7/2014 10:02 AM, Raffaele Sgarro wrote:
> §16 states:
>
> For every access of a local variable or blank final field x, x must be
> definitely assigned before the access, or a compile-time error occurs.
>
> Consider the following code:
>
> class  Ideone{
> 	
> 	public  interface  Provider{  String  get();  }
> 	
> 	public  static  class  Outer{
> 		
> 		private  final  String  data;
> 		private  final  String  token;
> 		private  final  Provider  secretProvider=  new  Provider()  {
> 			public  String  get()  {
> 				return  data;
> 			}
> 		};
> 		
> 		public  Outer()  {
> 			token=  secretProvider.get();
> 			data=  "FOOBAR";
> 		}
> 		
> 		public  String  getToken()  {
> 			return  token;
> 		}
> 		
> 	}
> 	
> 	public  static  void  main(String[]  args)  throws  java.lang.Exception  {
> 		Outer outer=  new  Outer();
> 		System.out.println(outer.getToken());  // Prints null
> 	}
> }
>
> Note that if I used a lambda expression, instead, javac would have
> complained.


More information about the compiler-dev mailing list