• 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
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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?
 
Bartender
Posts: 9493
184
  • 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: 2200
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: 3746
40
  • 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: 2200
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: 61702
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A wet cardboard box, surely.
 
Dave Tolls
Rancher
Posts: 3746
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Possibly with some sort of amphibian in it.
 
Campbell Ritchie
Marshal
Posts: 61702
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A newt?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!