Sobes.tech
Junior — Middle

Kas on võimalik muuta objekti sees efektiivselt lõplikult deklareeritud muutuja väärtust?

sobes.tech AI

Vastus AI-lt

In Java, the term "effectively final" means that a variable has not been changed after initialization, even though it is not explicitly declared with the final keyword. This is important, for example, when using such variables in lambda expressions or anonymous classes.

If a variable is declared as effectively final, its value cannot be changed — attempting to assign a new value will cause a compile-time error.

However, if this variable references an object, the state of the object can be modified, because what changes is not the reference, but the internal fields of the object. For example:

final List<String> list = new ArrayList<>();
list.add("new item");  // This is allowed

// list = new ArrayList<>();  // Error, cannot change the reference

Thus, the value of the variable (the reference) cannot be changed, but the contents of the object it refers to can be modified.