I'm working through the online tutorial and I keep coming back to static versus non static, especially as it relates to context. In the review exercise for Classes and Objects there is the following code and according to the tutorial the correct answer is to delete the 'static' modifier. I also realize that I can instantiate the classProblem object and refer to it refer to the 's' variable. The tutorial says "a static nested class cannot refer directly to instance variables or methods defined in its enclosing class " but I guess I don't understand why?
This obviously has to with the scope of the variables that is different from what I'm used to. Why can't a static nested class refer to variables in the outerclass without instantiating them? Maybe it's just me and I'm stuck on this, but I'm hoping if I understand why, I can move on beyond this issue.
Thanks for any help in advance.
public class Problem {
String s;
static class Inner {
void testMethod() {
s = "Set from Inner";
}
}
}
Thank you for any help in understanding this.