Answer the question
In order to leave comments, you need to log in
What are getters and setters used for in Java?
Good afternoon! Can you please tell a beginner what setters and getters are used for in Java?
The bottom line, I understand that they allow you to access variables of other classes with a private access level, but why then set the private access level to variables, if you can still read them from another class through getters and setters.
Thank you.
PS Strongly do not scold, just started to learn Java.
Answer the question
In order to leave comments, you need to log in
By the way, the question is not even stupid.
You are absolutely right about the violation of encapsulation by getters and setters, but no one is forcing you to write them. If you think that an object should be immutable, set all fields to final and initialize them only through the constructor. As for getters - also, depending on the business logic, they are not always needed.
On this topic, read the literature about OOP (for example, Weisfeld's "Object-Oriented Approach").
And yet, for example, Yegor Bugaenko has an opinion on this topic (it is worth treating him with healthy skepticism).
https://www.youtube.com/watch?v=lfdAwl3-X_c&t
There is no point if it is normal to program and build simple convenient code. A sort of way to formally enforce encapsulation and data hiding: is the state private? Private :)
They are fraught with the fact that they produce high cohesion in the code, the state is controlled from the outside (based on getters) and invariants are controlled anywhere and in any way. In fact, we turn entities into structures, but since these are still business objects, and knowledge of the business state is needed - then they walk around the entire project and are not limited by anything ... braiding and braiding code again and again, as well as plunging into these " essence" more and more knowledge...
It comes to the point that you can get a product through an order, you can get a supplier through a product, through a user supplier with authentication data ... and everything works with everything ... spawning a bunch of services and blowing up the brain ...
On the question
Only fucking fast CRUD are needed and for compatibility with a bunch of libraries. And also govnokoderam "use data in services"
Setters and getters are needed for various manipulations: validation, crutch mapping, serialization and deserialization.
It is now a good and modern practice not to use both
getters and
setters ... research:
Why (not) need getters?
Getters/setters and the problem with encapsulation (Symfony examples, php analog of Spring with similar bad practice)
but why then set the private access level to variables, if all the same, through getters and setters you can read them from another class.
In order not to have direct access to variables, access will be done only through certain methods (set and get). That is, you will have only two possibilities (return and set) the value of this variable. Other manipulations are IMPOSSIBLE.
but why then set the private access level to variables, if all the same, through getters and setters you can read them from another class.
The answer is obvious! To process the data! Properties (fields) do not know how to process, and getters and setters are methods (functions), the method can process this data and throw exceptions in case of misuse.
The bottom line, I understand that they allow you to access variables of other classes with the access level private
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question