+ 9

Better understanding of getters and setters - Java

Hi. I'm still confused as to how getters and setters work. I understand how paramaters and arguments work but not exactly this concept. From my understanding, getters are supposed to get a variable within the class? But what's confusing is when I think of 'getting something' I think of getting it and keeping it, however the getter method only has single statement 'return color' so then the other question is where is it returning it to? Back to the orginal private protected variable? Also the setter method.. I understand it uses parameters but where are the arguments coming from? public class Vehicle { private String color; // Getter public String getColor() { return color; } // Setter public void setColor(String c) { this.color = c; } } class Program { public static void main(String[ ] args) { Vehicle v1 = new Vehicle(); v1.setColor("Red"); System.out.println(v1.getColor()); } } Also, it seems a bit long to have to use two different methods, why not just use one method that gets and sets rather than creating two? One last thing, is it possible you could provide a short simple real world example of what would happen if we don't use getters and setters to protect the variable?

9th Aug 2017, 12:48 PM
YodaCoda
YodaCoda - avatar
11 Answers
+ 19
The reason to use getters and setters, rather than provide direct access to the variable, is to protect your data from outside access. This is called 'encapsulation'. Without the getters and setters, and if the variable color were public rather than private, you could just change it's value like this: vehicle.color = "black" (assuming you have an instance of Vehicle called vehicle). However, this would make it possible for any code to change the color of your vehicle. Using getters and setters, you maintain control of the color of your vehicle. This may not seems so important for the color of a Vehicle, but what about usernames, passwords, customer addresses, accountnumbers, etc? I'm sure you can think of real world examples where you wouldnt want just any code to be able to change your data...
9th Aug 2017, 1:37 PM
marit vandijk
+ 5
Your other question: the return returns the value to the method calling it. Think of it as asking the vehicle: "hey, what color are you?". It replies with whatever color. And you can go on your merry way and do whatever with this information... :)
9th Aug 2017, 1:39 PM
marit vandijk
+ 3
Finally, I understand! Thanks so much for that. I was stuck on this for days.
9th Aug 2017, 6:52 PM
YodaCoda
YodaCoda - avatar
+ 2
Yes, the getters and setters act as indirect ways to get and set the value of a private field. This way you can protect (encapsulate) your data. Also note that by not providing a setter the field cannot be changed. Or you can add any type of logic to the getters and setters. i.e. it can only be set to certain things, in certain cases, etc. Keyword 'this' refers to the instance you are calling a method on.
10th Aug 2017, 2:13 AM
marit vandijk
+ 1
Hey, why do we use "this.color = c;" instead of "color = c". Both seem to work fine for me. Also, in the real example I created just now I just realised that the getter method acts as a way for us to indirectly access the private 'password' variable, however, when I removed the setter method, I noticed how that's not relevant unless I wish to change (set) the password in the other class (Accounts). So my question is, from this example, how would I be able to change the change the password from the other class?: https://code.sololearn.com/cHZ8R34s8aAR/#java UPDATE: I think I did it. Is this how we indirectly set the private variables from another class. I always thought that only the class of the private variable had the power to change it not the other class: https://code.sololearn.com/cup26CZoujE4/#java
9th Aug 2017, 8:41 PM
YodaCoda
YodaCoda - avatar
0
you can have both in one method, true. programmer sometimes just wish to retrieve the value of an instance variable defined outside the method scope however. In such cases, programmers would find getter to be useful because those variables are not local in the method and setting them public to access directly is generally bad programming practices. The correct convention would be to use getter method to return the reference to such instance variable.
12th Aug 2017, 12:29 AM
Gao Xiangshuai
Gao Xiangshuai - avatar
0
//Answer for why we use this.color =c is explained below class shades { String color; shades(String color) { this.color=color; // if we dont use this statement it will consider the local string variable color so it won't assign. this is used for assigning local variable value to the outer variable } } class main { public static void main (String[] args) { shades s=new shades ("Red"); System.out.println("color is "+s.color); } }
12th Aug 2017, 9:33 AM
Lokesh Rajasekhar
Lokesh Rajasekhar - avatar
0
yes
12th Aug 2017, 5:34 PM
Sahil Khan
Sahil Khan - avatar
0
and why we can just use public String getColor(String color){ return color; } but we are using public String getColor(String c){ this.color = c; return color; } Thanks for help 😊😉
13th Aug 2017, 6:42 PM
Jakub Duchoň
Jakub Duchoň - avatar
0
in here there's no requirement for using this statement because the argument list and the variable are different (Color is not same as c). so we can directly program as public String getColor(String c){ color = c; return color; }
18th Aug 2017, 5:39 PM
Lokesh Rajasekhar
Lokesh Rajasekhar - avatar
- 2
ghb
11th Aug 2017, 3:08 PM
Ąłí Frãđj
Ąłí Frãđj - avatar