• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

here is doubt about initializing final local var...

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use Code Tags to keep your code indentation intact.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
 
Fire me boy! Cool, soothing, shameless self promotion:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic