Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

public and private class in the same file

Tags:

java

I created a public and private class in the same java file. It is not getting compiled.

However, if I keep a public class and a class without any access modifier, it is gets compiled.

What is the reason for this ?

like image 939
Sarath Avatar asked Dec 02 '25 10:12

Sarath


1 Answers

Toplevel private class is nonsense, because no other class can access this class. That is why you get compilation error..

JLS states that ;

The access modifiers protected and private pertain only to member classes within a directly enclosing class declaration (§8.5) and are discussed in §8.5.1.

like image 79
Gursel Koca Avatar answered Dec 04 '25 23:12

Gursel Koca