How do I know if a method will change the state of a java object?

Some methods are mutator methods, usually they do not return anything, the so-called setters. Others, such as the .plusDays () method of the LocalDate class, return a full instance of an object of type Localdate, so if you want to change the object, you need to point the existing object variable to the newly created one.

Is there a way to find out in advance whether the method will be a mutator or work as previously mentioned separately from finding its return value?

+4
source share
2 answers

No, there is no way to find out (despite the documentation or implementation) whether the method will change some state.

, void, , - ( ?), - , ( , , , JVM).

, , , -, .

immutable, , , " , ?" , - . , ErrorProne @Immutable , .

+5

, , void setProperty(PropertyType property), , (, -, , , ).

, Builders, (, Builder withProperty(PropertyType property)), , .

, , , ( ).

+1

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


All Articles