• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why is it giving me this error ?

 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic