E
E
EVGENY T.2018-03-06 13:57:42
Java
EVGENY T., 2018-03-06 13:57:42

What is the correct way to use class fields inside a class?

Question for Java programmers who work in companies where code reviews are carried out.
What is the correct way to use class fields inside the class: directly or through setters/getters?
For example:
Inside the class human there is a private field age that I want to use inside a method of the class human.
What is more correct:
if (age == 18) goArmy();
or
if (getAge() == 18) goArmy();
And the same question in the case of assignment: directly or through a setter?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Eremin, 2018-03-06
@Beshere

on the getAge() and other methods, you can always hang breakpoints and see in the debugger who, for example, rewrites the value.
For this reason, in our team, for example, we abandoned the simple state

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question