This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner class doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner class doubt" Watch "Inner class doubt" New topic
Author

Inner class doubt

raja kanak
Ranch Hand

Joined: Oct 18, 2006
Posts: 135

The above code shows error at //1 while compiling. I thought since a is declared as final, it can be used inside MethodLocalClass. But what went wrong?


live
Jesse Custer
Ranch Hand

Joined: Feb 07, 2007
Posts: 45
You are reassigning a final variable. You cannot give 'a' a new value.
raja kanak
Ranch Hand

Joined: Oct 18, 2006
Posts: 135
Originally posted by Jesse Custer:
You are reassigning a final variable. You cannot give 'a' a new value.


I changed //1 as
System.out.println(a);

Still it is problem, "Identifier expected".
ShivKumar Rajawat
Greenhorn

Joined: Mar 13, 2007
Posts: 17
Hi Live,
After this replacement your inner class structure becomes :

class MethodLocalClass {
System.out.println(a);
}
instead of puuting this statement directly inside the class , could you please try creating a member method and then put this statem,ent in that method.


Regards<br />-Shiv
raja kanak
Ranch Hand

Joined: Oct 18, 2006
Posts: 135

Thanks ShivKumar for your reply. As per your suggestion I modified my code as at //0 final int a; and at //1 included a=5 which gives compile time error "Cannot assign a value to final variable". So i commented //1 and again it complains that "variable a might not have been intialized".

That means we must initialize the variable a before the MethodLocalClass definition.

Again at //0 i removed final modifier, then the compiler error is "local variable a is accessed from within inner class; needs to be declared final"

Conclusion: To access a local variable inside inner class, that variable should be declared final as well as it should be initialized outside the inner class.

Please correct me, if I am wrong.

raja
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 407
    
    1

JSL.....

A blank final (�4.12.4) field of a lexically enclosing class may
not be assigned within an inner class.


and ...

Any local variable, formal method parameter or exception handler parameter used but not declared in an inner class must be declared final. Any local variable, used but not declared in an inner class must be definitely assigned before the body of the inner class.


Switch statements can be fun in what they do an don't allow with finals too.


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Inner class doubt
 
Similar Threads
MethodLocalClass Question
local inner class
Method Local Inner class
Method Local Inner Class