Getters/Setters vs Public Fields

This cluster debates the use of getters and setters for private fields in languages like Java versus public fields or properties in other languages like C# and Python, questioning their value for encapsulation, boilerplate, and design.

➡️ Stable 0.5x Programming Languages
2,327
Comments
19
Years Active
5
Top Authors
#569
Topic ID

Activity Over Time

2007
4
2008
36
2009
39
2010
107
2011
59
2012
111
2013
136
2014
170
2015
109
2016
122
2017
216
2018
136
2019
145
2020
133
2021
206
2022
155
2023
139
2024
151
2025
153

Keywords

OO OP XML LONG ANY MVC OOP IDE API JavaBeans getters setters getters setters java properties private methods access oo object

Sample Comments

gcb0 Feb 27, 2016 View on HN

seems like a very convoluted and inefficient way to have getter and setters without saying those words

grishka May 7, 2021 View on HN

Curious question — why do people insist so much on fields being private and there being getters and setters, even when all that getters do is return the field and all that setters do is set it? What kind of problem does this arrangement solve? Why not just use public fields?

memonkey Aug 18, 2014 View on HN

Sorry to ask, but as a noob who just picked up Java, how do other languages address this issue? What's wrong with getters and setters?

socialdemocrat Nov 26, 2020 View on HN

An article about why using setters and getters for member variables is frequently a bad choice.

xxs Oct 31, 2024 View on HN

Getters and setters are a mediocre design choice, not a limitation. Records have existed for years, too.

WClayFerguson Jan 16, 2021 View on HN

I probably have 1000s of plain getters and setters in my code, and I never typed ANY of them. lol. VSCode has a "Generate Getters and Setters" refactor function, and I can make them plain publics any time I want. So you can't blame the Java Language itself for their existence.

commandlinefan May 23, 2019 View on HN

It’s become dogmatic in Java to automatically create getters and setters for every single private variable in the class - include mutable objects like Lists and Maps (so a reference from a getter can actually change the referenced object). I’ve pointed out more than once that these getters and setters - usually auto-generated by an IDE or an XML compiler - serve absolutely no purpose and you might as well just cut out the middle man and mark the variables public at that point. Nothing makes a J

Kiro Feb 1, 2019 View on HN

Why do you need to write all these getters and setters? Why not just a public property?

Jach Dec 20, 2010 View on HN

Getters and Setters violate encapsulation just as much as accessing properties directly, so I don't bother with them unless I'm forced to. In languages like C++ they can make sense due to a need of allocating and freeing memory of things within the object, but it's still not OOP. What you have when you're using getters and setters is a fancy struct with methods, not an OO class. (There is a significant difference.) Stop pretending you're not using a struct and just access things directly or star

phkahler Mar 22, 2018 View on HN

Instead of attributes that lead to creation of getters/setters, how about the simple notion of private or public access to members? I'm not sure which should be the default, but it sounds like Java programmers would like them private unless otherwise specified. Why add code when all you want is to restrict access?