Should Getters and Setters Be Static?

Should Getters and Setters Be Static?

Getter and setter are just normal methods . They can be static or not . The only constraint is , do not use non-static filed and method in the static method. As static method and static filed belong to a class ,and non-static method and field belong to the object .

Why are setters and getters bad?

Getter and setter methods (also known as accessors) are dangerous for the same reason that public fields are dangerous: They provide external access to implementation details. ... You also have to change the accessor's return type. You use this return value in numerous places, so you must also change all of that code.

Should I always use getters and setters?

Using getters and setters, is always, in my opinion good practice. One thing you should avoid is to have external entities mess with the internal structure of your class at will. Typical example, consider having a dateOfBirth parameter.

David Miller
Author

David Miller

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.