Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested classes vs Separate class files

In java at least, my compiler won't let me put two (or more) separate classes in a single class file. It forces me to create multiple files in the same package. I understand why this is, and I agree with this principle. However, I've realized that I could just as easily nest the classes and contain the entire program (excluding imported libraries) in a single file. I would imagine that this would be frowned upon, but I don't understand why. What are the dangers and disadvantages of nesting class as opposed to the standard system (outside of the severe loss in organization and neatness)?

like image 946
Jean Valjean Avatar asked Sep 25 '22 13:09

Jean Valjean


1 Answers

Well, if you keep everything in the same file, you lose packages and the private keyword does no longer have any meaning.

Given that most Java projects will be opened in an IDE that presents different classes as different tabs, you can have more that one cursor in the program.

Why would you have everything in the same file?

like image 69
Tassos Bassoukos Avatar answered Oct 21 '22 17:10

Tassos Bassoukos