Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getter-Setter and private variables [duplicate]

If I can change the value of private variable through getter-returned reference then isn't it bypassing the setter method? Doesn't it defeat the purpose of getter-setter and private variables

public class Test{

private Dimension cannotBeChanged;

public Test(int height, int width)
{
    if(height!=3)
       cannotBeChanged.height = height;
    if(width!=3)
       cannotBeChanged.width  = width;
}

public Dimension getDimension()
{
    return cannotBeChanged;
}


public void setDimension(int height, int width)
{
    if(height!=3)
       cannotBeChanged.height = height;
    if(width!=3)
       cannotBeChanged.width  = width;    
} 

 public static void main(String [] args)
{
    Test testOne = new Test(5,5);
    Dimension testSecond = testOne.getDimension();
    testSecond.height = 3; //Changed height and width to unwanted values
    testSecond.width= 3;
}
like image 550
genonymous Avatar asked Feb 25 '14 08:02

genonymous


People also ask

What happens if getters and setters are made private?

setting them as private won't add any value because they will be not accessible from outside the class and accessible from inside without having the setters and getters at all. Therefore, the answer is: They must be public members.

Do private variables need getters and setters?

We require a setter method to update the value of a private variable from outside of the class and a getter method to read the value of that variable. It allows developers to control how the main variables in the code are accessed and updated.

What is the use of private variables even though they are accessible via getters and setters from some other class?

In classes, variables are often made private for encapsulation, and to limit the variables to a certain scope allow better error control and fewer bugs. This makes sense, as the fewer places a variable can be accessed the fewer places a bug can occur with that variable.

Can setter method private?

The private getter/setter methods provide a place for adding extra behavior or error checking code. They can provide a place for logging state changes or access to the fields. They can provide a place for adding your debug code while testing.


2 Answers

Yes, It does. I have the following conclusion in getters and setters from the Clean Code book; you can use it if you really accept it.

  1. Very evil: public fields.
  2. Somewhat evil: Getters and setters where they're not required.
  3. Good: Getters and setters only where they're really required - make the type expose "larger" behaviour which happens to use its state, rather than just treating the type as a repository of state to be manipulated by other types.
like image 118
Abimaran Kugathasan Avatar answered Oct 02 '22 03:10

Abimaran Kugathasan


Programmer should devise the ways for external entities to touch the secured variables of his program.

  1. Never create any setter for a secured property of your object. Only a getter can be provided.
  2. Create setters only for those properties, which can change during the course of program.
  3. Use setters if you want to apply certain restrictions on some properties e.g. apply invalid value checks, pre-population, logical analysis, populating another depending property, defensive copying etc
  4. Getters/setters helps in maintaining the software entropy of a system. Read about software entropy.
  5. Do not create getters/setters where it is not required as its leads to Boilerplate code.
  6. Getters/setters helps in changing the underlying implementation for future Extensions of Programs e.g. Upgrading Logging libraries etc
like image 39
InBravo Avatar answered Oct 02 '22 03:10

InBravo