This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android 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
Author

here is doubt about initializing final local var...

aditya makam
Greenhorn

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();
out1.method();
}
void method()
{
final String str1;
I obj = new I(){
public void setval()
{
str="aditya";
str1="makam";
}
};
obj.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
From: http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html

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
Sheriff

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
sscce.org
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;
 
 
subject: here is doubt about initializing final local var...
 
Similar Threads
What is the difference in String a = "abc" and String a = new String("abc")
equals() and ==, doubt
Question about Strings?
Interface instantiated
String literal objects in String pool