S
S
Stanislav2021-01-08 15:50:49
Java
Stanislav, 2021-01-08 15:50:49

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

7 answer(s)
D
Dmitry Roo, 2021-01-08
@xez

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

M
Maxim Fedorov, 2021-01-08
@Maksclub

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)

V
Vasily Bannikov, 2021-01-08
@vabka

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.

1. For example, so that you can add some validation logic inside the setter, for example.
2. To be able to use them in conjunction with interfaces

S
Siriosca, 2021-01-08
@siriosca

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.

S
Saboteur, 2021-01-08
@saboteur_kiev

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.

so that they cannot be read directly. That's the point of getters and setters.

N
Nickname192, 2021-01-11
@Nickname192

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.

P
pazukdev, 2021-01-11
@pazukdev

The bottom line, I understand that they allow you to access variables of other classes with the access level private

They allow controlled access. Any entity must be accessible to the external environment only through the interface. Public methods are the interface of any class. Setters and Getters are part of this interface.
Although accessors may indeed seem to be a formalism to some extent.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question