0

Quote from here, a static inner member class can

1) It can access static data members of outer class including private.

2) Static nested class cannot access non-static (instance) data member or method.

As I understand, a constructor is not static in java. But in builder pattern, a static inner class can also access the private outer constructor. Why is that, and what's the logic behind this design?

4
  • Constructors aren't static but can be called from a static context. Otherwise you couldn't use any of them. Commented Apr 11, 2017 at 10:13
  • Possible duplicate of stackoverflow.com/questions/23111745/… Commented Apr 11, 2017 at 10:14
  • 2
    The key takeaway from the linked question's answers is: Nested classes are members of the class enclosing them. Thus, like all members of that class, they have access to its private information. "Static nested class cannot access non-static (instance) data member or method." is just plain wrong. They absolutely do have access to private instance information -- provided they create an instance (perhaps with a private constructor) to access that instance information on. They don't have an enclosing instance like inner classes do, but they do have access to instance private members. Commented Apr 11, 2017 at 10:16
  • @bresai, there's no such thing in Java as a "static inner class", because the Java definition of an "inner class" is a nested class that is not static. Commented Apr 11, 2017 at 10:29

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.