I have a question, that represents interest to me. Suppose I have such a class definition:
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);
}
}
It seems like the compiler does not forbid such a code. Therefore I pass to HttpServletRequest instance a list of instances of inner class. But, then I try to access their public methods in JSP, I get a complaint about the fact that such read methods (for example, getName) do not exist. Although the objects themselves seem to exist.
What does it mean? That the inner classes cannot be passed to the outside world even implicitly? Or their life ends immediately, as the outer class instance stops existing? Or that nobody can access even public methods of an inner class, except the outer one.
Upd:
here is the fragment of jsp page that uses the list of instances of inner class:
<c:forEach var = "element" items = "${list}">
<c:out value = "${element.name}"/><br/>
</c:forEach>
It seems like forEach is working fine, but out cannot access public method getName() of inner class according to JSTL.
An instance of
Bcan not exist without the context of an instance ofA. If you have a reference to an instance ofB, that object implicitly has a reference to the enclosingAinstance, even if you do not directly.