• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Global final v/s Local final

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why do i get an error at linal 2 but no error at the second final declaration

public class test {
final int qss;

public static void main(String[] args) {}

void x(){
final int q;
}

}
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounded to me like this would be something pretty easy to find in the Java Language Specification. It was.

The JLS indicates that different checks are applied to final variables vs. final fields. For final variable the check is that it cannot be assigned to unless it is "Definitly unassigned" (There's a whole section describing what definitely unassigned means if you have questions about that see the (�16) link from section 4.5.4) For final fields compliler checks to make sure that it is definitly assigned (class variable by a static initializer, instance varibable by the end of the constructor).
8.3.1.2 final Fields

A field can be declared final (�4.5.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.5.4) class variable is not definitely assigned (�16.7) by a static initializer (�8.7) of the class in which it is declared.

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



4.5.4 final Variables


A variable can be declared final. A final variable may only be assigned to once. It is a compile time error if a final variable is assigned to unless it is definitely unassigned (�16) immediately prior to the assignment.


[ March 25, 2005: Message edited by: Carol Enderlin ]
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
  • Final variable which are data members have to be initialized when they are declared .



  • or you can initialize them in the constructor.


  • The compiler will issue a error for local variable (which can be final or non-final ) only if you try to use it in some expression or try to print it.

  • Just declaring local variable without initializing won't give compile time error.



    Hope that helps.
     
    Ranch Hand
    Posts: 1090
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi

    I also had tried this and got the same result. i don't know whether its a bug or a feature. What if you get this on the exam.

    Q : Can a final variable be left unassigned.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic