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

Answer by Lakindu Hewawasam for Why use getters and setters/accessors?

$
0
0

From my experience, it is ideal to set variables as private and to provide accessors and modifiers to each variable.

This way, you can create read only variables, or write only variables depending on your requirements.

Below implementation shows a write only variable.

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

Below shows a read only variable.

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

Viewing all articles
Browse latest Browse all 38

Trending Articles