Getter and Setter Method in Java Example
Getter and setter methods are frequently used in Java programming. Getter and setter methods in Java are widely used to access and manipulate the values of class fields. Usually, class fields are decorated with a private access specifier. Thus, to access them, public access specifiers are used with the getter and setter methods.
The Need of Getter and Setter Method
One may argue that declare the class fields as public and remove the getter and setter methods. However, such a coding style is bad, and one may put some absurd value on the class fields. Let's understand it with the help of an example.
Observe that the code is storing a negative salary in the database that is wrong. An organization never credits a negative salary to the account of an employee. Assigning an absurd amount to the salary variable happened because it is declared with a public access specifier. The correct way to write the above code is:
Now, we can see better control over what we send to the database to store. Whenever the salary is negative, we are converting the salary into a positive value, and then we are sending it to the database to store. Thus, no matter what value we send to the setter method, the if-block of the setter method takes care of the absurd value and thus gives better control on the salary value.
Getter Setter Java Program
Employee: [id = 107, name = Kathy, designation = Software Tester, company = XYZ Corporation]
Bad Practices in Getter and Setter Methods
There are some common bad practices that people usually do when they deal with the getter and setter methods.
Bad Practice 1:
Using getter and setter for the variable that is declared with low restricted scope.
It is evident that from the main method, one can directly access the variable salary, which is not only bad but also makes the presence of the getter and setter methods irrelevant.
Bad Practice 2:
Using an object reference in the setter method. Consider the following program.
3 4 6 8 78 9 -1 4 6 8 78 9
References are a bit tricky to deal with! In the above code, at line 43, the value got updated at the 0th index for array mainArr. However, it also got reflected in the array val. It should not happen as val array is declared private; hence, it is expected that any code outside of the class ABC should not modify it. However, because of the references, everything is messed up. The setter method setVal() expecting a reference of an int array, and at line 7, the reference of the int arr is getting copied to val. Note that the reference variable arr is storing the reference of the array mainArr. Thus, we can say val is storing the reference of the mainArr.
Therefore, whatever we change in the mainArr also gets reflected in the val array, which violates the purpose of the setter method. Also, there is no meaning in adding the private access specifier to the val array; because one can change the value of the val array in the main method, which is evident by looking at the output.
A better way of writing the above code is:
3 4 6 8 78 9 3 4 6 8 78 9
In the above code, we are doing the deep copy of elements of the array arr. In line 11, we are creating an entirely new array. Thus, the val is not referring to the arr. Also, in line 17, only values of the element are getting copied. Therefore, when we change the value of the 0th element at line 53, the change is not reflected in the val. Thus, the above code respects the encapsulation of the private member variable val.
Bad Practice 3:
Returning an object reference in the getter method. Observe the following program.
67 43 68 112 70 12 -1 43 68 112 70 12
The above code is not handling the references properly. The getter method is returning the reference of the array. The arr is storing the reference of the array val, which is declared private in the class ABC. Because of exposing the reference to the outer world, arr can manipulate the val, and thus, the encapsulation of the class ABC is breached. The proper way to handle the above is:
67 43 68 112 70 12 67 43 68 112 70 12
Explanation: In the above code, the reference of the private array is not sent to the outside world. In the getter method, a new array is created whose reference is sent to the main method. Therefore, when the value at the 0th index gets changed at line 54, that change impacts the temp array, not the private array val. Thus, the encapsulation of the class ABC is maintained, as the reference of the array val is not exposed to the outside world.
Note 1: For primitive data types (int, char, etc.), one does not need to create a copy in the getter and setter methods, as the concept of references is absent for the primitive data types.
Note 2: Strings object types also work on the references. However, unlike the above examples, one does not need to take care of the String references exposed to the outside world. It is because Strings are immutable. Thus, when one manipulates the string in the main method (or anywhere else), a new String object is created, and the previous one remains untouched.
The String is: Hello India! The String is: Hello India!