I know that Java doesnt allow a top level class to be private however is there any way that the private classes are allowed apart from top level class?
Asked
Active
Viewed 4,177 times
-1
-
2Did you try it? – home Nov 27 '16 at 17:04
-
1I tried it for top level class but compiler error as - illegal modifier for the class. – geekNava Nov 27 '16 at 17:09
-
1So, other than top-level classes, what kind of classes are there? *(that was a hint question)*. Did you try making any of *them* private? – Andreas Nov 27 '16 at 17:11
-
1I just tried inner / nested class ...thanks for responding! – geekNava Nov 27 '16 at 17:12
2 Answers
4
You could use nested classes.
Link: https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

Tacolibre
- 110
- 2
- 12
-
2Thanks, I have gone through the link and understood that inner / nested class can be private. – geekNava Nov 27 '16 at 17:11
-
0
A class is a user-defined (custom) datatype and you can't declare a class
in Java as private
, but if you do not want to expose a particular user-defined data type (class) outside of another public
class, then you can declare that as a nested/inner class (i.e., as a member of the public
class). You can refer here for details.

Vasu
- 21,832
- 11
- 51
- 67