Guys, I’ve come across such legal behaviour:
File B.java:
final class C {}
final class D {}
File A.java:
class B {}
public class A {}
Questions:
- When class X is required to be placed into its own X.java file? Does class visibility/final matter here?
- Is there any official spec on this class/java relation?
Thanks a lot.
The de-facto standard in most implementations is that a source file can only contain one top-level
publictype definition. The name of the source file must be the name of that type.A source file can contain nested types, but by definition they’re not a top-level
publictype.This is recommended in, but not required by, the Java Language Specification.
Note that
finalhas nothing to do with accessibility, so it’s not a relevant issue in this matter.Related questions
See also