1

I am a beginner programmer and I am having a hard time grasping getters and setters. I just do not see the point.

I am trying to access the variable in Class A and use that value in class B to do some function. I thought I could use getter to access that value but that returns null since I understand that I am creating a new object with new values now. Then what is the point of a getter then?

I passed the variables over using the method parameters but that seems counter intuitive to my beginner's mind. I just don't understand that entire concept. Or am I wrong. I can use getters to access the value of another class's variable without making it static?

DuyN
  • 11
  • 1
  • 2
    Possible duplicate of [Why use getters and setters/accessors?](https://stackoverflow.com/questions/1568091/why-use-getters-and-setters-accessors) – crash Oct 11 '19 at 19:51

1 Answers1

0

If I'm understanding you correctly, what you're asking is "Why do I need instance variables, with getter and setter methods to read/modify those variables, when I can just pass data into an object using method arguments?" Does that sound about right?

The answer gets to the heart of what OOP (object-oriented programming) is all about. The central concept of OOP is that you create distinct objects to represent discrete pieces of data. For example, you might want to track names and ages for some group of people; in that case, you would use different objects to represent (and by extension, store and manage data about) each individual person.

Person 1 ("Bill", 52)

Person 2 ("Mary", 13)

Person 3 ("Lana", 29)

The purpose of the class in this model is simply to define the specifications of these objects (e.g. a "Person" consists of a name and an age).

Why is this useful? First, this is a pretty intuitive system, since you can think of the objects you're creating as being actual real-life objects. Second, it makes it easy to work with data that are related. If, for example, we wanted to concatenate (join together in a string) a person's name with their age, having an object representing each person makes that easy! Just look at each object, one by one, and use getters to access the values for each instance.

To do this in a non-OOP way, we would need some other way to store the information -- perhaps as a list of names and a separate list of ages.

List of names: ["Bill", "Mary", "Lana"]

List of ages: [52, 13, 29]

In that kind of setup, it's not as easy to see which name relates to which age -- the only thing they have connecting them is their position within the list. And if the lists were sorted, those positions could change!

So, in short: object instances are a great way to handle many similar discrete collections of data.

As far as why we generally use getter methods and setter methods when working with those instances, instead of just exposing properties directly, a great explanation can be found here. But it bears mentioning that different languages handle this differently. In JavaScript, for example, all properties are accessible directly. In Ruby, none of them are, and you must use setters and getters to see/modify object instance variables.

I hope this provides some clarity!

Matt B
  • 116
  • 6