I am reinitializing the member variable of a local class and am getting a compilation error. What is wrong here? Why will this not compile? Thanks!
This will not compile
public class TestSomething {
public void someMethod(){
class LocalClassInner{
int i=100; // Error on this line.Syntax error on token ";", , expected
i=200;
}
}
}
This compiles just fine
public class TestSomething {
public void someMethod(){
class LocalClassInner{
int i=100;
//i=200;
}
}}
This isn’t really an local class problem. You can’t put assignment statements at the “top-level” of any class.
Assignment statements need to go in a method, constructor, static initializer, or instance initializer.
WHen you wrote
you were actually declaring a field of the inner class (as you know). You can reassign this field, provided you did so in a constructor, method, etc.
ADDENDUM
The following uses an instance initializer, just for fun:
It compiles fine, see http://ideone.com/qjnv3