aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why is it giving me this error ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why is it giving me this error ?" Watch "why is it giving me this error ?" New topic
Author

why is it giving me this error ?

gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

Please consider the following snippet of code



why it gives compiler error. shouldn't it just give default value 0 to i variable ? if i remove final it works fine.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

gurpeet singh wrote:Please consider the following snippet of code



why it gives compiler error. shouldn't it just give default value 0 to i variable ? if i remove final it works fine.


This is required by the Java Language Specification.... specifically, section 8.3.1.2.

http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.3.1.2

A field can be declared final (§4.12.4). Both class and instance variables (static and non-static fields) may be declared final.

It is a compile-time error if a blank final (§4.12.4) class variable is not definitely assigned (§16.8) by a static initializer (§8.7) of the class in which it is declared.

A blank final instance variable must be definitely assigned (§16.9) at the end of every constructor (§8.8) of the class in which it is declared; otherwise a compile-time error occurs.


Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
jQuery in Action, 2nd edition
 
subject: why is it giving me this error ?