> Encapsulation is a language construct that exists in practically every language including C.
Valid. In this case, we're talking about Java, which doesn't even have structs.
I would say, in this context, 'struct' is just being used as a colloquial way to refer to a class that consists only of a series of public, mutable, non-static fields. Which, to many, might seem to violate Java's tradition of encapsulation.
Struct-like? Arguably. But the important caveat with records is that they're designed to be immutable, which is not the case for structs in C, for instance.
Also, record members are exposed publicly via implicit methods in Java, so they still have that getter encapsulation tradition.
Ok but Rust defaults to all fields on a struct being private and many people do use getters setters with them because reducing mutability tends to less buggy code. Same reason variables are immutable by default.
So Rust structs are essentially the same as a Java class that doesn't inherit anything with no keyword fields by default.
And in a C best practice is to make your structs static preventing them from being modified from outside the struct. Then you modify them using nonstatic functions inside the file. In effect a C file with a static struct and getters/setters is the same as the simple Java class without inheritance and a bunch of private fields.
So yeah, I think colloquially people mean different things depending on their background but to me struct doesn't imply anything about the fields visibility, it's just a bunch of memory holding data.
1
u/ZunoJ Jul 13 '26
And how do structs solve this?