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

What is the main difference between an inne...

Câu trả lời

The main difference between an inner class and a static nested class in Java lies in their relationship with the outer class and their access to its members.

A static nested class is a nested class that is declared with the static modifier. It does not require an instance of the outer class to be instantiated and cannot directly access the instance (non-static) variables or methods of the outer class. Static nested classes are associated with the outer class itself, rather than with any object of the outer class, and they can only access the static members of the outer class directly[1][2][3][5][6].

On the other hand, an inner class is a non-static nested class that is associated with an instance of the outer class. It can access both static and non-static members of the outer class, including private ones. Inner classes require an instance of the outer class to exist and can manipulate the fields and invoke methods of the outer class as they maintain a reference to an instance of the enclosing class[1][2][3][4][5][6].

In summary, the key distinctions are:

  • Instantiation: Static nested classes can be instantiated without an instance of the outer class, while inner classes require an instance of the outer class.
  • **Acces...
senior

senior

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

junior

What’s the difference between sendRedirect and forward methods?

middle

Why does Java have transient fields?

junior

What is an Iterator?

Bình luận

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

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