• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

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: 9137
172
  • 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: 2180
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: 3596
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: 2180
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: 59762
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A wet cardboard box, surely.
 
Dave Tolls
Rancher
Posts: 3596
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Possibly with some sort of amphibian in it.
 
Campbell Ritchie
Marshal
Posts: 59762
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A newt?
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!