Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Only one public class in one file in Java

 
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: 12254
259
  • 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: 2233
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • 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.

 
Master Rancher
Posts: 4665
49
  • 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: 2233
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • 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: 70261
282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A wet cardboard box, surely.
 
Dave Tolls
Master Rancher
Posts: 4665
49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Possibly with some sort of amphibian in it.
 
Campbell Ritchie
Marshal
Posts: 70261
282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A newt?
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic