3 ms·
Java doesn't enforce 1 file = 1 class but rather 1 file = 1 public class, which is exactly what you asked for. You can put as many private classes in the file a
by nradov 11y ago
Java doesn't enforce 1 file = 1 class but rather 1 file = 1 public class, which is exactly what you asked for. You can put as many private classes in the file as you want.
- brandon73 11y agoThis isn't accurate either. Java does not enforce 1 file = 1 public class but rather 1 file = 1 public top-level class. For example this is totally legit: // ClassA.java public class ClassA { public static class ClassA_Inner_Public_Static { } public class ClassA_Inner_Public { } } // ClassB.java public class ClassB { ClassA classa = new ClassA(); ClassA_Inner_Public classA_Inner_Public = new ClassA().new ClassA_Inner_Public(); ClassA_Inner_Public_Static classA_Inner_Public_Static = new ClassA_Inner_Public_Static(); }
- MaulingMonkey 11y agoNot exactly - I didn't ask for enforcement, and the irritation usually arises from wanting the occasional secondary type - such as an enum - to be part of the public interface (thus forcing the type itself to be public, naturally) without either: 1) moving it into a separate file, for the simplest two value one liner or 2) making it interior to the other class, with all the verbose lengthy names (and semantic implications) that this might entail. I want clear and concise names, so I lean towards #1. And if an enumeration gets more shared between multiple classes, or collects enough values and comments to merit it, sure, I'll move it into it's own file. But forcing me to shove it in a separate file before I've even decided on the best name (meaning I need to rename the file repeatedly as well), and before I'm even certain I'll keep that specific enumeration, is just meaningless friction.