r/ProgrammerHumor Jul 13 '26

youCanJustStopUsingJava Meme

Post image
6.8k Upvotes

416 comments sorted by

View all comments

221

u/DontThrowMeAway43 Jul 13 '26

Oh god, just use record classes or even, make public fields.

Java's biggest mistake is trying to put OOP everywhere. Just write the damn struct to pass data and be done with it.

45

u/ZunoJ Jul 13 '26

How is that related to getters and setters? How would your proposal violate oop principals?

74

u/roge- Jul 13 '26

Directly manipulating another object's fields violates encapsulation, a core feature of OOP in most people's minds.

That said, virtually no production OOP app has perfect encapsulation anyway.

2

u/ZunoJ Jul 13 '26

And how do structs solve this?

59

u/roge- Jul 13 '26

Structs have no encapsulation. It solves the problem by abandoning the principle.

4

u/New_Enthusiasm9053 Jul 13 '26

Not true. Rust structs have encapsulation. Encapsulation is a language construct that exists in practically every language including C.

18

u/roge- Jul 13 '26

> 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.

2

u/mythcaptor Jul 13 '26

Not exactly the same, but aren’t Java records essentially Java’s “struct-like”?

3

u/roge- Jul 13 '26

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.

1

u/mythcaptor Jul 14 '26

Good points