1

Problem

In an Android project, I have some private variables in a public class, the warning messages keep reminding me of implementing the getter and setter instead of assigning values direct to those private variables.

Question

When should getters/setters be implemented for private variables?

Source Code

MainActivity.java

public class MainActivity extends Activity implements OnTouchListener {
    // There are a few private variables that I used in the following methods in this class MainActivity
    private boolean _isMenuHidden = true;
    private boolean _isMenuMoveTriggered = false;
    private int _xDelta = 0;
    private int _yDelta = 0;
    private int _xTouchDown = 0;
    private int _yTouchDown = 0;
    private int _xPrevious = 0;
    private int _yPrevious = 0;

    // Some code omitted....

    // Implements View.OnTouchListener
    @Override
    public boolean onTouch(View view, MotionEvent event) {
        final int X = (int) event.getRawX();
        final int Y = (int) event.getRawY();

        view = (View) view.getParent();
        LinearLayout.LayoutParams mLayoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();

        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                _xTouchDown = X;
                _yTouchDown = Y;
                _xPrevious = X;
                _yPrevious = Y;
                _xDelta = X - mLayoutParams.leftMargin;
                return false;
            // case MotionEvent.ACTION_DOWN:
            //break;
            case MotionEvent.ACTION_MOVE:
                if (((X - _xDelta) < -384) || ((X - _xDelta) > 0)) {
                // The move allows at most 480 * 80% = 384 pixels leftward and
                // zero pixels rightward
                } else {
                      // Some code omitted....
                  }
                break;

            // Some code omitted....

        } // switch ()
    } // End of public boolean onTouch()
    // Some code omitted....             
} // End of public class MainActivity

Thank you :D

2
  • 1
    Setters are not OO. You should define operations that update internal state, not expose internal state. eg moveTo(x,y) not setX(x);setY(y). They were intended for an easy way to update properties from gui editors, not as the general practice they have become.
    – BevynQ
    Jul 10, 2013 at 2:20
  • @BevynQ, points taken, thank you :D
    – George
    Jul 10, 2013 at 2:28

3 Answers 3

4

Create public getters and setters when you want to allow classes to access your classes private data.

Create protected getters and setters when you want to allow only inheriting classes to access the private data.

You don't need to create getters and setters to access private data from the same class, but many people think it creates code that will be easier to maintain in the future, as requirements change.

Another reason for getters and setters is where the form or use of the data can change, for example your might have private data to store the birthdate, but create a getter named getAge().

1

Getters and setters should be implemented for private member variables of a class when you need to control the access and modification of their data. For example, if you have a zip code private field, you would want to make it private and write a setter that accepts a String, validates it, and only sets the member value if the zip code is legal and appropriate.

Doing so helps prevent garbage data from becoming an issue in your instances, as well as ensures encapsulation of your class.

0

Getters and setters are used to access private attributes of a class. The encapsulation is an OOP concept that dictates the following: - Attributes must be private (or protected) - The presence of setters and getters to allow the access to those private attributes from the outside of the class.

The setter ensure the coherence of the state of the class (the values of its attributes.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.