posted 14 years ago
I think the more relevant point, in this case, is that a private field is private to whatever top-level class it's contained within. If it's declared in a nested class ("Days" in this case), that private field is still accessible outside the nested class, but inside the containing top-level class ("Enums" in this case).
Why did Java do it this way? I don't know. It's been this way since nested classes were introduced, I think. Well, at least since JLS second edition. Nested classes had a lot of ambiguities before that.
By coincidence, we had a closely related discussion in my Scala reading group at work today. One of the differences between Scala and Java access specifiers is that in Scala, private means private to whatever class immediately contains the thing you're declaring as private. Scala's creator evidently thought this was more logical - you're scoped to the closest set of matching containing braces. But then Scala also lets your specify more esoteric options, like private[this] or private[OtherClassName] or private[some.package.name]. So I think Scala gives a more logically consistent default, while also letting you do other things if you think you need to. Meanwhile in Java, many people still don't realize that private variables can be accessed in other classes, as long as you're still in the same top-level class. It's one of those Java gotchas that occasionally bites people.