Getters and setters- help | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Getters and setters- help

I am able to implement getters and setters if given a specific scenario but i dont understand it. Why do we use them? when do we use them? How does it work? Can someone explain using the most simple language. An example would be great as well.

14th Apr 2017, 6:24 PM
Mogammad Shameer Losper
Mogammad Shameer Losper - avatar
4 Answers
+ 14
Though getter/setter methods are commonplace in Java, they are not particularly object oriented (OO). In fact, they can damage your code's maintainability. Moreover, the presence of numerous getter and setter methods is a red flag that the program isn't necessarily well designed from an OO perspective
14th Apr 2017, 6:15 PM
Dev
Dev - avatar
+ 4
+1 to Martin. It's the first time (and place where) I see Dayve on the wrong side. Setters are good in "protecting" from unwanted changes (as in examples given here: month, age etc), when your classes are used by other modules (by other people) and even by you when you can make mistakes (as in second example) and there are advantages when you are starting to use multithreading (advantages in using getters too). I'm not sure that I can give nice explanation and good example... but let me try. :) You can assure that value that some other object got by the getter stays unchanged until some "signal" (from another thread may be?) Update and question: I wouldn't call Set and Get "properties". They are methods. Example/code given by Skalpe02 looks like C#. Am I right? It's a little bit different in java (and " java" is the tag of the question).
15th Apr 2017, 1:36 AM
Andrew Harchenko (Tomsk)
Andrew Harchenko (Tomsk) - avatar
+ 2
First of all, you should understand the encapsulation. Set and Get are properties. For example,you have such kind of code. public class Date { private int month = 7; // Backing store public int Month { get { return month; } set { if ((value > 0) && (value < 13)) { month = value; } } } } We use properties in order to access the encapsulated fields, By the way,such way we can add the certain logic(see if). With get we receive the Field value,and with set we set the value. What else.. Besides,you should understand that such properties are shortcut variations of simple methods. It's easy to use )
14th Apr 2017, 6:24 PM
Nikita Galchenko
Nikita Galchenko - avatar
+ 1
Getters and setters are methods used for your attributes you have in yout class defined. In fact all of your atttibutes have to be private(in most cases) that other classes dont have acces to them so you cant directly change them from outside. so you have setters, wich means you can change the value of the private attribute but not directly. you ask yourself why do i need to change it by setter method instead i can change it directly and save time and it would be much easier. As i red some comments that theese methods are not well Object oriented and not good at all etc... i say you one... total bullshit Example ... public class Person { public int age, } public class Office { public void setPersonAge(Person p) { p.age="lol", } } As you can see you have your age aztribute public so all the other classes can modify them but look at the office class and method setPersonAge where it was added to int attribute string value so your program will crash. Of course you can add some statements where you will "cure" the error but what if you will have 600 classes that will have to modify the parameter age? You ll have to modify all methods in all 600 classes where it will be modified and that is something you don t want to so therefore you ll create one setter method and you can use it in all other classes... the more you will be working with java the more you will realise that getters and setters are the main building units not in java but in whole Objected oriented programming world!
15th Apr 2017, 12:12 AM
Martin Ďurčo
Martin Ďurčo - avatar