Câu hỏi phỏng vấn Java
Câu hỏi

What's wrong with Double Br...

Câu trả lời

What's Wrong with Double Brace Initialization in Java?

Double brace initialization, also known as anonymous inner class initialization, is a Java language feature that allows you to create an instance of an anonymous inner class and initialize its fields in a single expression. While it can be a concise way to create and initialize objects, it has several drawbacks that make it generally considered an anti-pattern in Java development.

1. Memory Leaks

The primary issue with double brace initialization is that it can lead to memory leaks. When you create an anonymous inner class, it has an implicit reference to the outer class instance. This means that the anonymous inner class instance will hold a reference to the outer class instance, preventing it from being garbage collected even after the outer class instance is no longer needed. This can lead to memory leaks and potentially cause performance issues in your application.

2. Readability and Maintainability

Double brace initialization can make your code less readable and harder to maintain. The syntax is not immediately intuitive, and it can be difficult for other developers to understand what's happening, especially in larger or more complex code bases. This can make it harder to debug and refactor your code in the future.

3. Lack of Flexibility

Double brace initialization is a limited feature that only allows you to initialize an object's fields in a single expr...

expert

expert

Gợi ý câu hỏi phỏng vấn

middle

What is the difference between doGet() and doPost() ?

middle

What are untrusted applets?

middle

What is difference between Array and ArrayList ? When will you use Array over ArrayList ?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào