• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Why only one public class per file?

 
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have some code here that contains something I've always wondered about:

If line 12 was not commented out, it would cause the compiler to throw the error, "The public type Calc must be defined in its own file". That being said, my current assumption is that the inner class Calc still has an invisible default access modifier, making it available to other classes in the same package.

Why is that allowed if having two public classes in the same file is not allowed?
 
Sheriff
Posts: 27451
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That rule makes it easier for the Java compiler to find the source code for class Thingy; it must be in a class named Thingy.java. This can (or maybe could) happen if you compiled a different class and it referred to Thingy, but Thingy.java hadn't been compiled yet. So the compiler would go ahead and compile that as well.
 
This is awkward. I've grown a second evil head. I'm going to need a machete and a tiny ad ...
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic