wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Global final v/s Local final Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Global final v/s Local final" Watch "Global final v/s Local final" New topic
Author

Global final v/s Local final

Nikhilesh Fonseca
Ranch Hand

Joined: Aug 23, 2004
Posts: 57
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;
}

}
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
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 ]
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
  • 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.


    Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
    Anupam Sinha
    Ranch Hand

    Joined: Apr 13, 2003
    Posts: 1088
    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Global final v/s Local final
     
    Similar Threads
    Question about Final class
    final variables
    valid combinations of Modifiers like static,native,transient etc
    List of tricks to watch for in SCJP questions - please contribute
    switch case with final