-4

Are there any alternative solutions for avoiding Getter/Setter methods in Android?

I am already using getter/setter methods but below link said, avoid getter/setter method.

How to access collection object using without getter/setter google solution?

anacron
  • 6,443
  • 2
  • 26
  • 31

3 Answers3

1

Usage of getters and setters is a very long argument. There are reasons for and against. Saying that using setters ensures encapsulation simply isn't true in most cases.

There is no difference between:

object.x = value;

and

object.setX(value);

Some examples of discussions here:

Advantage of set and get methods vs public variable

Why use getters and setters?

Are getters and setters poor design? Contradictory advice seen

Community
  • 1
  • 1
theblitz
  • 6,683
  • 16
  • 60
  • 114
0

From the link you provided:

It's reasonable to follow common object-oriented programming practices and have getters and setters in the public interface, but within a class you should always access fields directly.

From this we learn 2 things:

  1. NEVER make your fields public, this doesn't respect the OOP principles;
  2. You should use the getter/setter only when you are outside the scope of a class. When you are writing code inside the class you should always access the field directly. Here is the access speed for direct access:

Without a JIT, direct field access is about 3x faster than invoking a trivial getter. With the JIT (where direct field access is as cheap as accessing a local), direct field access is about 7x faster than invoking a trivial getter.

Iulian Popescu
  • 2,595
  • 4
  • 23
  • 31
  • I disagree - Don't over design your code. No need to strictly follow OO principles for your own code. If you are publishing a library for others to use, then follow OO and implement getter/setter. For your own code skip getter/setter until it is required. Getter/Setter bloat your code make it harder to maintain (every time you add or remove a variable which is shared you add or remove several lines of code per variable). Exceptions always exist so limit public access to simple objects. – LanDenLabs Sep 02 '17 at 21:52
  • If you say that you don't need to use OO principles for your own code that means that you don't really care about that qualitty of your own code. Also, by saying `skip getter/setter until it is required` you broke that open/closed principle, which might not matter in your opinion but surely will give you a lot of headache when trying to change from variables to methods. There was a reason why all this OO principles where though like this. – Iulian Popescu Sep 03 '17 at 08:59
0

From the link you provided:

However, this is a bad idea on Android. Virtual method calls are expensive, much more so than instance field lookups. It's reasonable to follow common object-oriented programming practices and have getters and setters in the public interface, but within a class you should always access fields directly.

Basically what it means is that you can have getters and setters, but when you want to access a class' own fields, access it directly. In other words, you should do this:

class Foo {
    private int bar;

    public int getBar() { return bar; }
    public void setBar(int value) { bar = value; }

    public someOtherMethod() {
        // here if you want to access bar, do:
        bar = 10;
        System.out.println(bar);

        // instead of:
        // setBar(10);
        // System.out.println(getBar());
    }
}
// but outside of this class, you should use getBar and setBar
Sweeper
  • 213,210
  • 22
  • 193
  • 313