Getter and Setter Methods | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Getter and Setter Methods

Give a description to write a getter and setter method

26th Oct 2017, 2:31 PM
A M
A M - avatar
2 Answers
+ 3
If you read the chapter, it'll tell you how to write the getter/setter methods. However, I'll try to explain it a bit. Basically, when you have a class you'll have private members of the class that you don't want people directly accessing. Setters are methods that you can call from the object in order to change those private variables without directly accessing them. The Getters are methods you can use to indirectly return the value of those variables. This is called encapsulation and it helps you keep those variables hidden away. Hope that helps out some with understanding what they are. EXAMPLE: private String name; public void setName(String name) { this.name = name; } public String getName() { return this.name; }
26th Oct 2017, 3:31 PM
AgentSmith
+ 1
Thank u for your input!!
26th Oct 2017, 7:02 PM
A M
A M - avatar