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?

Community
  • 1
  • 1
bresai
  • 369
  • 5
  • 18
  • Constructors aren't static but can be called from a static context. Otherwise you couldn't use any of them. – biziclop Apr 11 '17 at 10:13
  • Possible duplicate of http://stackoverflow.com/questions/23111745/static-nested-class-has-access-to-private-constructor-of-outer-class – Robin Topper Apr 11 '17 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. – T.J. Crowder Apr 11 '17 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. – Lew Bloch Apr 11 '17 at 10:29

0 Answers0