How are getter and setter different from regular functions?

private String gg; public void setgg(String gg) { this.gg = gg; } public String getgg() { return gg; } 

Given the code above, setter and getters are used to deal with private members of a class.

Question1. If the setter accepts another parameter, I don’t think it is a setter?

question2. How do they differ for ordinary public member functions that set the values ​​of private data members?

I know that we can implement validation in setters to reuse code and eliminate exceptions, but still fail to understand the real purpose

+4
source share
6 answers

Question1. If the setter accepts another parameter, I don’t think it is a setter?

It will set the value, but it will not be the standard setter method that many frameworks look for to set the value

question2. How do they differ for ordinary public member functions that set the values ​​of private data members?

These are common public methods with a standard naming convention.


Cm

+4
source

Getters and seters are just an object-oriented convention. Many frameworks will look for methods called "getX ()" and "setX (type)"

They are no different from any other method, but adding or removing something from the method header violates the convention.

+4
source

First, your implementation of setter setter is wrong by convention. This should be setGg and getGg.

To answer your questions.

  • Getter installation methods have become the standard way to enter values ​​for objects, followed by many frameworks. So you need to follow the naming convention as above.

  • While it takes the same parameter and sets the values ​​to the same. But if you do not follow the naming convention, then most frameworks will not be able to enter values ​​to objects.

+2
source

Getters and seters have taken root as the de facto standard in the Java world for encapsulation: the idea that an object hides its internal implementation from class association. This is a general principle of object-oriented programming.

Consider the following class as an example:

 // boilerplate public class A { public String b; } 

This is a valid single-field Java class. If another class wants to read the BA property, all it needs is:

 A a; String val = ab; 

and for recording:

 ab = val; 

The problem is that A no longer controls writing to the internal state. Suppose we learned that b has a maximum length of 1024 characters. Each instance of the entry on b must now be updated to meet the new requirement. Depending on the size of your code base, this can be very expensive.

As an alternative, suppose we used the getter and setter convention to encapsulate b:

 // boilerplate public class A { private String b; public void setB(String val) { this.b = val; } public String getB() { return this.b; } } 

Now it looks like this:

 A a; String val = a.getB(); 

and writes:

 a.setB(val); 

It will take longer to write in the short term, but changing between plans is much easier to integrate by simply changing the setter.

Encapsulation is so highly regarded that, as many other commentators have pointed out, many Java frameworks expect this.

+2
source

As others have explained the goal of getters and setters. I would like to indicate that the syntax in your code snippet does not fit the java bean standard.

 private String value; public void setValue(String value) { this.value=value; } public String getValue() { return value; } 

for boolean

 private boolean val; public void setVal(boolean val) { this.val= val; } public boolean isVal() { return val; } 
0
source

This was asked and answered a long time ago, but still sent an answer to help future readers. Addition to what other participants have already answered.
Getters and setters differ from ordinary functions as follows:

  • Usually they do not contain business logic, unlike conventional methods.
  • Recipients have no parameters passed to it
  • Setters should only have one parameter passed
  • Used only to set and retrieve class attributes, which can be POJO, DAO, beans, etc.
  • As others have already mentioned, they are used by systems such as Hibernate, Spring, etc., to store, retrieve data, and perform many other operations.
  • Used to implement two basic concepts of encapsulation and abstraction.
0
source

Source: https://habr.com/ru/post/1434758/


All Articles