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

Get and set

what are get and set, what is their use. please explain in detail.

27th Feb 2018, 7:54 PM
Shubh Patni
Shubh Patni - avatar
6 Answers
+ 3
When dealing with OOP languages, there is something that's called encapsulation. Basically, it keeps your class's members private and isolated to the class, which makes it much more secure. Getters and setters are how we access those members from other code so that we can either set data to those members or get data from those members since we can't directly access those variables. EXAMPLE::::: private int num; // Getter - retrieves the data from num public int getNum() { return this.num; } // Setter - sets data to num public void setNum(int num) { this.num = num; }
27th Feb 2018, 8:14 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 3
You're welcome Shubh. I'll be more than happy to explain. this. = keyword that is referencing the current instance of a class (object). As an example, I think of classes/objects like this: A class is a blueprint (or a template) for an object that you can create in the world. The blueprint maps out exactly what the object is, its properties, and its functionality. The blueprint isn't the object itself, but merely something we can use to create as many objects as we want from it. When you instantiate an object, you're using the blueprint (class) to create an object in the world based off the blueprint. If you create 5 objects from that blueprint (class), although they appear to be the same object, they're each individual from one another and each interact with the world around them individually. So what happens to one object isn't going to affect any of the other objects, and it isn't going to affect the blueprint. (This isn't ALWAYS true, but you'll learn more about static stuff later) I can do as I wish with each object, and if needed destroy one while leaving the others intact. Okay, now that I'm done explaining the basic concept of classes/objects, lets get back to the keyword 'this.' 'this.' is referring to whatever object is being interacted with at that moment. So based upon the other example, if I decided to call the getNum() function from an object, 'this.' is referring to the value that's being stored in THIS object, rather than the other objects or the class. EXAMPLE: MyClass class1 = new MyClass(); MyClass class2 = new MyClass(); MyClass class3 = new MyClass(); class1.getNum(); class2.getNum(); class3.getNum(); ^In this example, I created 3 objects. When I call the getNum() function from each one, it's going to return the value that's being stored in the variable on that particular object. This helps ensure we're retrieving the correct value that we're seeking from the object. Hope that helps!
27th Feb 2018, 8:29 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 2
beyond that code
27th Feb 2018, 8:20 PM
Shubh Patni
Shubh Patni - avatar
+ 2
thank u so much
27th Feb 2018, 8:33 PM
Shubh Patni
Shubh Patni - avatar
+ 1
thank u
27th Feb 2018, 8:17 PM
Shubh Patni
Shubh Patni - avatar
+ 1
can u please explain me the use of this keyword
27th Feb 2018, 8:20 PM
Shubh Patni
Shubh Patni - avatar