0

Java specification says:

Static nested classes are accessed using the enclosing class name

Yet, I'm able to access and reference the static nested class WITHOUT specifying the enclosing class name:

Test.java (in some other package)

public static void main(String[] args) { //referencing the static class without the enclosing class name! Builder b = new Builder(); //ok! no need for "Response.Builder" } // also from a non-static method public void foo() { //referencing the static class without the enclosing class name! Builder b = new Builder(); //ok! no need for "Response.Builder" } 

What am I missing here? Does not this contradict the spec?

Response.java (the enclosing class) and Builder.java (the static nested class):

public class Response { public static class Builder { public Builder() { } public Response build() { // build Response instance } } } 
5
  • Maybe you have import at the top. Commented Jun 6, 2016 at 11:36
  • Please post a complete example. Commented Jun 6, 2016 at 11:37
  • you have static import.... Commented Jun 6, 2016 at 11:38
  • 1
    I do have a regular import: import com.mycompnay.Response.Builder; is this the reason? Commented Jun 6, 2016 at 11:43
  • 1
    Your link is to a tutorial, not to the language specification. Commented Jun 6, 2016 at 11:47

1 Answer 1

1

Referring to your comments: Static imports are used to reference static methods and fields directly. For types, regular imports are the way to go, also for static nested types.

Remove the import statement and your code will break.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.