3 ms·
One of the best improvements in Kotlin (over Java) is the ability to have multiple top-level classes, functions, and static variables in one file. I hate jumpin
by tadfisher 3y ago
One of the best improvements in Kotlin (over Java) is the ability to have multiple top-level classes, functions, and static variables in one file. I hate jumping between 10 files when reviewing code.
You technically can do this in plain Java, but it comes with many caveats and probably won't work with a default build setup. And no, nested classes are not the same thing.
- beeboobaa 3y ago> And no, nested classes are not the same thing. They actually are if they're public static. Quite literally so, nested static classes are the exact same as "normal" classes but named `OuterName$InnerName.class`
- occz 3y agoIt might be for the compiler, but the nesting implies hierarchy for the reader, which may or may not be desirable.
- beeboobaa 3y agoSame goes for putting them in the same file.
- dtech 3y agoThey are not the same at all. One practical reason is that in Kotlin (and other languages supporting this) you can always move the class to a dedicated file if it or the file becomes to unwieldy. With a nested class all the imports would need to change, which is a no go unless you can refactor all code using it in 1 go.
- beeboobaa 3y agoThis only matters if you are building a library intended to be consumed by other projects, and you are intending to release before deciding on a public API, and for some reason put multiple classes in a single file. Pretty specific case of a bad practice. Otherwise, just use your IDE's refactor capabilities.
- deleted 3y ago[deleted]