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.

Avoiding escaping collection references

Avoiding escaping collection references - Java Tutorial

From the course: Java Memory Management: Values and References

Avoiding escaping collection references

- [Instructor] So, we just created a copy to deal with escaping references of collections. But we can also do something different. We can create an unmodifiable collection. This way we are using a special method, actually, one of the special methods, to create a collection that can not be altered. This will solve the problem for collections with immutable objects as a build in Java solution. This way the collections can not be modified. And if there are immutable objects on there, well, they can not be changed anyways. So there's no problem anymore. But it doesn't solve the escaping references of immutable objects on collections though. Let's have a look at how to create unmodifiable collections in IntelliJ. So, where we created the copy we now just send all the product's objects to a new array list. I'm going to do something different right now. I'm going to be returning an unmodifiable list. And I can use the list or the collection's interface to do this. Let me show you two ways of…

Contents