Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't we define a top level class as private?

Tags:

java

Why does Java not allow a top level class to be declared as private? Is there any other reason other than "We can't access a private class"?

like image 820
Satya Avatar asked Dec 16 '09 10:12

Satya


People also ask

Can top-level classes be private?

No, we cannot declare a top-level class as private or protected. It can be either public or default (no modifier).

Can we make outer class as private?

As a member of the OuterClass , a nested class can be declared private , public , protected , or package private. (Recall that outer classes can only be declared public or package private.)

What happens if class is made private?

Explanation: If we make any class constructor private, we cannot create the instance of that class from outside the class.


1 Answers

A top-level class as private would be completely useless because nothing would have access to it.

like image 185
Maximilian Mayerl Avatar answered Oct 16 '22 22:10

Maximilian Mayerl