Quantcast
Channel: Why use getters and setters/accessors? - Stack Overflow
Viewing all articles
Browse latest Browse all 38

Why use getters and setters/accessors?

$
0
0

What's the advantage of using getters and setters - that only get and set - instead of simply using public fields for those variables?

If getters and setters are ever doing more than just the simple get/set, I can figure this one out very quickly, but I'm not 100% clear on how:

public String foo;

is any worse than:

private String foo;public void setFoo(String foo) { this.foo = foo; }public String getFoo() { return foo; }

Whereas the former takes a lot less boilerplate code.


Viewing all articles
Browse latest Browse all 38

Latest Images

Trending Articles





Latest Images