+ 1

Using set and get methods in java

Hi everyone, in my programming class, the teacher said we have to implement a class and use get methods but did not mention set methods in the description. Are they always used or should be used together? The link below is what I've completed so far. Appreciate any help https://code.sololearn.com/ceWT6DZromw3

17th Oct 2021, 2:41 PM
David
David - avatar
3 Respostas
+ 2
Martin Taylor thank you for responding. This helps a lot.
17th Oct 2021, 3:33 PM
David
David - avatar
+ 1
Why we should use Getter and Setter methods? This question confuse every new programmer who learn Encapsulation in oops.  Why we should use getter and setter methods if we can directly access and use variable using defining public? Why we use getters and setters? So, simple answer is we set limit in variable access using getter and setter methods. Like if anyone wants to get or set above name variable then there is only one option using getName() and setName() method. Another main use of getter and setter, that we can put any validation in these methods. like in above example if we want to set name that is not have greater than 10 length. so we can easily add these type of validation in above method. so, anyone who want to set name then they must have to set name in less than 10 length. https://uniquethrowdown.blogspot.com/2020/12/what-are-oops-concept-in-java.html
19th Oct 2021, 2:53 PM
Java Developer
Java Developer - avatar