What is the point of the get and set method

In my CS class, I just study classes and OOP.

Therefore, when you create a class, you initialize a certain amount of the private variable.

I know that you make them private, because if they were public, they change easily and can lead to many errors.

So, we use the get and set methods to change the variable. But does it again make variables very easy to change correctly? So, what's the point of making them private in the first place?

0
java oop class encapsulation
Oct. 16 '14 at 2:00
source share
8 answers

Some advantages of using getters and setters (known as encapsulation or data-hiding ):

1. Class fields can be made read-only (providing getter only) or write-only (by providing only setter). This gives the class full control over who gets access to / change their fields.

Example:

 class EncapsulationExample { private int readOnly = -1; // this value can only be read, not altered private int writeOnly = 0; // this value can only be changed, not viewed public int getReadOnly() { return readOnly; } public int setWriteOnly(int w) { writeOnly = w; } } 

2. Class users do not need to know how the class actually stores data. This means that data is shared and independent of users, which makes it easier to modify and maintain code. This allows maintainers to make frequent changes, such as bug fixes, design and performance improvements, without affecting users.

In addition, encapsulated resources are equally accessible to each user and have identical behavior, not dependent on the user, since this behavior is internally defined in the class.

Example (getting value):

 class EncapsulationExample { private int value; public int getValue() { return value; // return the value } } 

Now, what if I want to return the value twice? I can just change my getter, and all the code that uses my example does not need to be changed and will get twice as much value:

 class EncapsulationExample { private int value; public int getValue() { return value*2; // return twice the value } } 

3. Makes the code cleaner more understandable and understandable.

Here is an example:

No encapsulation:

 class Box { int widthS; // width of the side int widthT; // width of the top // other stuff } // ... Box b = new Box(); int w1 = b.widthS; // Hm... what is widthS again? int w2 = b.widthT; // Don't mistake the names. I should make sure I use the proper variable here! 

With encapsulation:

 class Box { private int widthS; // width of the side private int widthT; // width of the top public int getSideWidth() { return widthS; } public int getTopWIdth() { return widthT; } // other stuff } // ... Box b = new Box(); int w1 = b.getSideWidth(); // Ok, this one gives me the width of the side int w2 = b.getTopWidth(); // and this one gives me the width of the top. No confusion, whew! 

See how much more control you have, what information you get and how much clearer it is in the second example. Keep in mind that this example is trivial and in real classes you will have to deal with a lot of resources that are accessed by many different components. Thus, the encapsulation of resources makes it clearer which of them we are referring to and how (getting or setting).

Here is a good SO thread in this section.

Here is a good read for encapsulating data.

+3
Oct 16 '14 at 2:09
source share

As stated above, getters and setters encapsulate (i.e. hide) the internal details of your class. Thus, other classes that interact with yours do not need to know about the implementation details.

For example, in the simple case that you are describing, instance variables are displayed through getters and setters. But what if you want to change your class so that you no longer use instance variables, but rather store the values ​​on disk. You can make this change in your class without affecting users of your class.

Keep in mind also that getters and setters do not always have to be provided. If you do not want your class to provide a way to set or read these properties, then do not. Just make them private.

+1
Oct 16 '14 at 2:05
source share

get is used to get the value for the attribute, and set is used to send the value to the attribute for example:

 private int variable; public int getVariable(){ return variable; } public void setVariable(int aux){ variable=aux; } 

Generally used to encapsulate an attribute.

link:

Install and get methods in java?

0
Oct. 16 '14 at 2:05
source share

Encapsulating or hiding data gives you more control over what values ​​can be set in the field. Here is an example if you do not want the class attribute to have a negative value:

 class WithoutGetterSetter { public int age; } class WithGetterSetter { private int age; public setAge(int age) { if(age < 0) // don't set the value else this.age = age; } } public class testEncapslation { public static void main(String args[]) { WithoutGetterSetter withoutGetterSetter = new WithoutGetterSetter(); withoutGetterSetter.age = -5; WithGetterSetter withGetterSetter = new WithGetterSetter(); withGetterSetter.setAge(-5); } } 
0
Oct 16 '14 at 2:06
source share

The Get and Set methods are preferable to "public" variables, since they isolate class users from internal changes.

Suppose you have the variable "StockQty", and you have made it publicly available, because it seemed like the easiest thing to do.

You will later receive a user request to track stock history over time. Now you need to implement the SetStockQty () method so that you can save the old quantity somewhere before setting the new quantity.

Now all users of your class must change the code, re-document and retest.

If you had the SetStockQty () method, then only you would need to modify and test your code.

The second reason is that you have Getters without Setters, which makes the variable read-only.

0
Oct. 16 '14 at 2:09
source share

Traditionally, they are justified in terms of encapsulation. By providing moderate access to read and write the fields of the class, we presumably reduce communication.

In a simpler language: by controlling the ways in which other classes can read and modify our data, we reduce the ways that our class data is modified. This means that connections between classes are reduced, which reduces complexity.

However, the same logic says that getters and setters should generally be avoided if they are not needed for them, and very rarely such a need. For the most part, the class should "strive for its own knitting" - if there is a calculation that needs to be done for the data of this class, it should do it. If a value needs to be changed, it must change.

For example, consider an object in space. It has a location indicated as (x, y, z). We could let other classes just install them arbitrarily - that would be awful, obvious, but not obvious that the installer would be better for them. What you really want is a constructor that sets the starting position, and then methods that influence that position - for example, to record a hit or acceleration. Then you program OO.

0
Oct 16 '14 at 2:11
source share

One word, Encapsulation.setters also allows you to control how values ​​are entered into your program. Many new programmers like me often mix with this concept. I urge you to read this SO question

0
Oct. 16 '14 at 2:14
source share

Be objective: all about best grades !!!

1) IF necessary, set your attributes using get methods. 2) IF necessary, enable attribute modification (state modification) using dialing methods;

Do the public methods for receiving and installing without treatment are the same as the public attributes.

0
Oct. 16 '14 at 2:16
source share



All Articles