• Post Reply Bookmark Topic Watch Topic
  • New Topic

Only one public class in one file in Java  RSS feed

 
Greenhorn
Posts: 13
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why can't more than 1 public classes be present in a single JAVA file?
 
Saloon Keeper
Posts: 8720
162
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because that's just how the designers wanted it to be.

They probably did it to reduce the amount of time that compilers need to find out what file a class description is in.
 
Ranch Hand
Posts: 231
12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:They probably did it to reduce the amount of time that compilers need to find out what file a class description is in.


The JLS supports your insightful comment

This restriction makes it easy for a Java compiler to find a named class within a package.


For example, the source code for a public type wet.sprocket.Toad would be found in a file Toad.java in the directory wet/sprocket, and the corresponding object code would be found in the file Toad.class in the same directory.

 
Bartender
Posts: 2156
46
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

For example, the source code for a public type wet.sprocket.Toad would be found in a file Toad.java in the directory wet/sprocket, and the corresponding object code would be found in the file Toad.class in the same directory.



Or:
- the source code for a public type wet.sprocket.Toad would be found in a file sprocket.java in the directory wet, and the corresponding object code would be found in the file sprocket$Toad.class in the same directory.
- the source code for a public type wet.sprocket.Toad would be found in a file wet.java in the . directory, and the corresponding object code would be found in the file wet$sprocket$Toad.class in the same directory.

 
Rancher
Posts: 3441
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the dev who named their classes either 'wet' or 'sprocket' would be found out on the kerb, holding the contents of their desk in a cardboard box.
;)
 
Paweł Baczyński
Bartender
Posts: 2156
46
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:And the dev who named their classes either 'wet' or 'sprocket' would be found out on the kerb, holding the contents of their desk in a cardboard box.
;)


The problem is java and javac don't know about this.
 
Marshal
Posts: 58823
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A wet cardboard box, surely.
 
Dave Tolls
Rancher
Posts: 3441
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Possibly with some sort of amphibian in it.
 
Campbell Ritchie
Marshal
Posts: 58823
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A newt?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!