Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Static nested classes in Java

I'm unsure why this code compiles... quoting the Java tutorials:

like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class — it can use them only through an object reference.

Src: http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

public class StaticNested {
 private String member;
 private static String staticMember;
 static class StaticNestedClass {
 private void myMethod() {
 System.out.println(staticMember);
 StaticNested nested = new StaticNested();
 System.out.println(nested.member);
 }
 }
}

I didn't expect to be able to access member directly, but the code compiles fine. Am I misunderstanding the Java spec?

Sorry about the formatting, I'm struggling with my browser + post editor.

Answer*

Draft saved
Draft discarded
Cancel

lang-java

AltStyle によって変換されたページ (->オリジナル) /