Like in title. Why are any modifiers allowed inside private classes in the first place? The private class is not visible outside the file, so we cannot access them anyway. If the visibility matters inside such classes, which is the recommended one?
Asked
Active
Viewed 134 times
2
-
Related (same question about "package-private" classes): http://stackoverflow.com/questions/2049718/what-is-the-use-of-having-public-methods-when-the-class-is-having-a-default-acce?rq=1 – Thilo Mar 06 '15 at 12:15
-
http://stackoverflow.com/questions/5260467/public-methods-in-package-private-classes – assylias Mar 06 '15 at 12:16
2 Answers
3
Your private class can still implement a public interface (or extend an accessible class).
Or maybe just override toString
, equals
or hashCode
(all of which have to be public).

Thilo
- 257,207
- 101
- 511
- 656
-
If i get it right, your point is that access modifiers exist in private class so you can implement / extend and the compiler wouldn't allow limiting the visibility of methods. But still, I cannot use such methods outside the file so all the methods / fields could be implicitly set to public (so there won't ever be limiting visibility) – Please Mar 06 '15 at 12:27
-
1Why cannot you use these methods outside of the file? You can have a private class implement `Comparator` or `Runnable` or `Collection` and return an instance of it from your method. Code calling that method can invoke `compare` or `run` or `size` on your private class instance. – Thilo Mar 06 '15 at 12:30
-
I might be doing it wrong, but in my test, when a private class implements an interface, the complier won't allow me to use such method when I retreive the object in another file (The type Test.Nested is not visible) – Please Mar 06 '15 at 12:34
-
The type `Test.Nested` is not visible, but you can type it as `Object` (or `Runnable` or whatever interface it has). `Object x = test.methodReturnsMyPrivateInstance(); x.toString();` – Thilo Mar 06 '15 at 12:36
-
1@Please you can't access the type directly outside the containing class, no, but if you have `private class MyComparator implements Comparator
` you can return an instance of that class from a `public Comparator – Ian Roberts Mar 06 '15 at 12:38createComparator()` method and the caller can assign the returned reference to a variable of type `Comparator `. -
You guys are right, seems I did it wrong ;) And as to new fields / methods (not the ones from iterface or super class), is there any difference there? – Please Mar 06 '15 at 12:44
-
There is no point in making the "new" fields/method public. But it's not illegal either. (it might actually be useful for reflection or some tools like the ones @Evgeniy mentions) – Thilo Mar 06 '15 at 12:46
1
One difference: privateClass.getMethods will return only public methods
Another one: some tools, like JAXB, may be looking for public setter/getters

Evgeniy Dorofeev
- 133,369
- 30
- 199
- 275
-
1
-
-
So, should I set all the methods / fields to public, just because some tools might require it? – Please Mar 06 '15 at 12:42
-
1Only if you are going to use those tools, otherwise visibility makes no real difference – Evgeniy Dorofeev Mar 06 '15 at 13:02