From the course: Java Memory Management: Values and References

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Final classes and immutable objects

Final classes and immutable objects - Java Tutorial

From the course: Java Memory Management: Values and References

Final classes and immutable objects

- [Instructor] Variables that are final cannot be changed. These values are immutable and can only be assigned a value once. When you assign a new value to them, the code will not compile, but this doesn't change anything for the place where they are stored on the stack though. It also doesn't change anything on how they are passed around. Only the value gets passed around and nothing else, and this means they are no longer final after having been passed around, since only the value gets passed and nothing else, so the parameter in place where it's being received, it's not final, it's just a value. Because of this, immutable objects are a slightly different story. Immutable objects are objects whose values cannot be changed after creation. This means that the variables on immutable objects are final and cannot be changed from inside or outside. Let's go ahead and have a look at IntelliJ to see an example of an immutable object. So here we are seeing an immutable example object. It has…

Contents