What happens to an instance of the inner class if it is passed to the jsp page

I have a question that is of interest to me. Suppose I have a class definition like this:

public class A extends DumbAction{ public class B { // Inner class definition public String getName() {return "Class B";} } @Override public void execute(HttpServletRequest request) { ArrayList<B> list = new ArrayList<B>(); list.add(new B()); // Here I add some more elements request.setAttribute("list", list); } } 

The compiler does not seem to prohibit such code. Therefore, I pass to the HttpServletRequest instance a list of instances of the inner class. But then I try to access their public methods in the JSP, I get a complaint that there are no such reading methods (like getName). Although the objects themselves seem to exist.

What does it mean? That inner classes cannot be passed into the outside world even implicitly? Or does their life end immediately as an instance of the outer class ceases to exist? Or that no one can access the public methods of the inner class except the outer.

Upd: here is a fragment of a jsp page that uses a list of instances of the inner class:

 <c:forEach var = "element" items = "${list}"> <c:out value = "${element.name}"/><br/> </c:forEach> 

It seems that forEach is working fine, but cannot access the public method getName () of the inner class according to the JSTL.

+4
source share
1 answer

Instance B cannot exist without the context of instance A If you have a reference to instance B , this object implicitly has a reference to instance A , even if you are not using it directly.

+5
source

All Articles