Java: the need to use private variables and return methods

In the code I saw, some people often use private variables, for example.

private static int number; 

And they usually have access methods like

 public static int returnNumber(){ return number; } 

But my question is, what's the point? How do i do it

 int number; 

Following this, when I need to access it

 int numberToBeAssigned = someClass.number; 

instead

 int numberToBeAssigned = someClass.getNumber(); 

It seems to me impractical to use access methods and private variables, I know what they do, access to private variables is allowed only to the class in which they are located. I just don’t see the need for them when you can just as easily instantiate a class and call its member variable when you need it. I am obviously mistaken in my logic, but I would like someone to give a clear example of how private variables can be used together with access methods.

thanks

+4
source share
5 answers

An access point like this should allow you to reverse engineer the implementation without breaking the rest of your code. For example, what if later you decide that number should come from a file? Or need to be moved to another class?

If you have limited access to the accessory, you can make such changes, and you only need to change the accessor - you do not need to change all the other code that depends on it.

+6
source

This is all about encapsulation. Open fields allow any ol class to come and change your values ​​and possibly violate your invariants. If you only have public int foo , anyone can install it. If this is a private int foo , and you provide a getter, people can get it. If you provide a getter and setter, then they can do both, but you still control it; you can reject a change that violates the assumptions that the class must fulfill (or wants).

You may not have such assumptions in this class right now, but a) you may have some in the future and b) other classes have these assumptions, and therefore it is necessary to protect their private fields, and it is good to have a consistent way of accessing data ( in some cases not fields, getters and setters in others).

Indeed, this concerns the basic principles of OO: your class is not just a state state, it is something that has properties and actions, and there is a state under the hood to reflect these actions. So, for example, when you request List length() , you do not conceptually care that it was indicated as a cached value, or the List repeated its elements and counted them; you just want its length.

This leads us to the last big point (at least in this answer :)), which is that the getter and setter can be overridden to take advantage of inheritance, while the field cannot.

+2
source

Here is a perspective from wikipedia :

Hiding the internal objects of an object protects its integrity by not allowing users to set the internal data of the component to an invalid or inconsistent state. The advantage of encapsulation is that it can reduce the complexity of the system and, thus, increases reliability, allowing the developer to limit the interdependencies between software components.

0
source

Many books have been written on these questions; the answer cannot be fully explained in the answer format on stackoverflow. This is actually very important. In short, the fewer appraisers you provide in your code, the more opportunities you must change during refactoring without damaging the code of those who use it. Your code becomes clean and reliable, you can achieve a higher level of immutability, you can make a better architectural solution.

0
source

Short answer: You want to minimize unwanted interactions by publishing as few components as possible, so that it still does its part and ensuring that the component works as close as possible in the application, since it behaves in isolation (there, where it can be easily tested).

Longer answer: Type methods represent his contract, while his variables represent his state. Any nontrivial type will contain several member variables with their own interrelated states. As the evolution offered by this type (its contract) develops (new or advanced functions, bug fixes), the programmer must keep in mind all these relationships in order to make sure that the existing behavior does not break.

A well-established way to achieve this is to write Unit Tests, but this only confirms the type in isolation. By minimizing the presentation of the type state and limiting it to well-understood and planned operations, the programmer tries to minimize model failures that he or she was able to verify.

In contrast, if external components have ways to access the state of the component beyond those planned by the developer of this type, it is easy for them to inadvertently disrupt its behavior. And the interactions between the components are exponential: in my experience, when a program spans over 20,000 lines of code, the author of several developers, each of which specializes in separate modules, and goes through several versions, no one will have an ideal idea of ​​the whole code anymore.

Many books and articles have been written on this subject, and I am sure that they will be more eloquent and convincing than I can be. For general Java recommendations, I highly recommend Josh Bloch Effective Java (2nd Edition).

0
source

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


All Articles