File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes here is doubt about initializing final local var... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "here is doubt about initializing final local var..." Watch "here is doubt about initializing final local var..." New topic

here is doubt about initializing final local var...

aditya makam

Joined: Mar 30, 2006
Posts: 9
Hi friends,

This is the program.

interface I
void setval();
class Outer
String str;
public static void main(String arop[])
Outer out1= new Outer();
void method()
final String str1;
I obj = new I(){
public void setval()

Its giving compile time error as shown below.Can anybody explain the reason?

waiting for your reply,

aditya makam

Be Happy and Enjoy Life.
J Sato
Ranch Hand

Joined: Mar 30, 2006
Posts: 40

When an inner class refers to an instance variable that is a member of a lexically enclosing class, the variable of the corresponding lexically enclosing instance is used. A blank final (�4.5.4) field of a lexically enclosing class may not be assigned within an inner class.

If you move the declaration for str1 inside the setval() method, the code will compile.
[ April 06, 2006: Message edited by: J Sato ]
marc weber

Joined: Aug 31, 2004
Posts: 11343

Please use Code Tags to keep your code indentation intact.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Changchun Wang
Ranch Hand

Joined: Feb 15, 2006
Posts: 83
define a final variable in the method ,you must initlize it
java don't have is blank local final variable
so the final local variable is different from general local variable (not final variable )
the general variable can initlizew before using it;
I agree. Here's the link:
subject: here is doubt about initializing final local var...
jQuery in Action, 3rd edition