Primitive and non-primitive types of getters / setters and initialization

Are Java primitive (object) data types handled in the same way as primitive data types? By treatment, I mean ...

1: should they be closed in class?

2: Should they be initialized to some value during declaration? (e.g. String S = "" instead of String S )

3: Should they have getters and setters too?

+4
source share
8 answers

The simple answer to all three questions is yes. These three practices sound regardless of whether the variable in question is of a primitive type.

One of them should know that if you have a reference to a mutable object and your getter returns that link, this potentially allows the caller to modify the object (which, in your opinion, was private to your class!)

Another difference between the two cases is that object references can be set to null . This can be useful, but it can also be a source of errors. It may be worth noting that String s = null and String s = "" do not match.

Finally, with objects, it is important to understand the difference between obj1 == obj2 and obj1.equals(obj2) . This difference does not occur with primitive types, since there is only one way to compare them for equality.

+4
source

to 1: of course, that the principle of information hiding

to 2: many of them are already initialized "under the hood", but you have to do it yourself

to 3: of course, information is also hidden

Best, Flo

+4
source

I donโ€™t know of any rule that says that any particular attribute should be private in the class, Java offers other modifiers, such as protected and public, and no modifier that implies the availability of the package. All of them are there, so you can apply different levels of encapsulation , which you can consider suitable in accordance with your design.

Regarding the initialization of the question field, I believe that when the field is declared in the class, if the field has any reference type it is default initialized to null , so you do not need to initialize it if you do not consider it necessary, or if the field is not declared final, which means you want to initialize it to its default value.

On the question of how to get and establish part of the question, I believe that this is just a way to provide encapsulation. Allan Snyder, in his article Encapsulation and Inheritance in Object-Oriented Programming Languages, wrote:

To maximize the benefits of encapsulation, you should minimize the identification of implementation details in external interfaces [...] For example, one characteristic of an object-oriented language is whether it allows a designer to define a class so that its instance variables can be renamed without affecting clients.

Also, a great article by Leonid Mikhailov and Emil Sekerinsky described the study of the fragile base class problem may show some good ideas on why all these levels of encapsulation and indirection are appropriate to avoid some of the classic inheritance problems.

โ€œThere is no direct access to the state of the base classโ€: the extension of the class should not directly access the state of its base class, but only by calling methods of the base class.

Their article provides very good reasons why something like the getter and setter methods might be a good idea to avoid class vulnerabilities.

+3
source

The answer to the whole question is yes. You describe here the general principle of data encapsulation and a bean naming convention that is type independent.

+2
source

1: whether they should be closed in class.

Yes. For what reasons, you should declare primitive typed variables private.

2: whether they should be initialized to some value during the declaration (for example: String S = "" instead of String S)

It depends. If the uninitialized state ( null ) has an application-specific value, you should not initialize by default. Otherwise, it is best to initialize a nonzero default value if possible.

3: Must also have receivers and setters.

Yes. It is for the reasons why you should declare getters in a primitive-typed case. You must also declare setters, but only if you want the variable to be customizable.

+2
source

Specifically, object type variables must be private, initialized, and have getter and seters, respectively. This allows OOP to keep the code safe, but still accessible.

+2
source

1: whether they should be closed in class. Yes 2: Should they be initialized with some value during the declaration (for example, String S = "" instead of String S) Yes 3: Must have both receivers and setters. Yes

+2
source

In relation to 1. and 3. the general principle is data encapsulation and is not related to the data type (primitive or non-primitive).

Regarding initialization: class reference variables are initialized to null by default. Other than that, they should just be initialized no matter what value matches the design of your class.

+1
source

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


All Articles