3 ms·
The default scope of everything is package level. If one writes all of their code in one directory, Java is basically procedural code with syntactic sugar for f
by AmpsterMan 5y ago
The default scope of everything is package level. If one writes all of their code in one directory, Java is basically procedural code with syntactic sugar for functions with structs as the first parameter. The moment you want to share with other packages, one has to use the public keyword. I'd argue what Java 'wants' you to do is to have a few public methods that operate on internal state, but it doesn't care too much if state is shared within a package. A good Java API wouldn't have many Setters/Getters that do nothing but set/get references to internal objects, but rather ones that parse and make sense of it's internal state.
In practice, I've found most codebases don't care about any of that so every variable is private, they all have public getters/setters (so it's NOMINALLY encapsulated) and most methods are public with a few private methods strewn about.
- dmux 5y agoThanks for the explanation. I got caught up on the "message passing" vs "api" part of your original comment and was wondering how you differentiated between the two. >A good Java API wouldn't have many Setters/Getters that do nothing but set/get references to internal objects, but rather ones that parse and make sense of it's internal state. I completely agree. Getter/Setter obsession often leads to breaking the Law of Demeter in most Java code bases I've seen.
- AmpsterMan 5y agoI'm not the original poster so you should wait for their reply, hah! They may have meant something else. This is just a topic I've spent some time thinking on and wanted to share my own $0.02. My go-to language for personal projects/small tools at work is Java. I think if one looks at Java codebases as strict encapsulation at the package level it becomes a lot more bearable.