Can we declare class as 'private' in java?

4.4k Views Asked by At

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?

2

There are 2 best solutions below

1
On
0
On

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.