0

I try to define two classes with public access modifier. But i got an error that the classes should be defined in respective java files.

my question is Why cant we have two public classes in one java class file?

i understood the error i just want to know , why cant we do it ? i mean the reason behind it

Thanks

Ravi Jadhav
  • 81
  • 1
  • 11

1 Answers1

1

I think this could explain why in a very simple way:

Because if you did, the name of the java file wouldn't be the same as the class name.

However, be sure to understand that is valid:

public Car{
 public Motor{

 }
}

Here you can read more about it.

You can read more about it in this interesting oracle forum discussion.

Hope it helps.

Community
  • 1
  • 1
Cacho Santa
  • 6,846
  • 6
  • 41
  • 73