Page# 139 on the Head First EJB says the following:
Remember, you always have to cast and narrow a stub unless the method that returns it has the actual Remote interface as its declared return type.
The declared return type of the getEJBObject method of the Handle interface is itself "EJBObject". Then why do we need to narrow and cast while calling the getEJBObject method over the handle object?
I have still some more confusion. I tried serializing the handle and then deserializing and using it - without narrowing, just casting. Following the short code snippet for AdviceClient and AdviceClient2
AdviceClient
AdviceClient2
Note above that I have just used plain old casting, (no narrowing).
Now, I initially ran the AdviceClient. Then I undeployed the app and shutdown the j2ee server. Then after some time, I started the j2ee server back up, deployed the app, and after that I ran the AdviceClient2, and it worked.
EJB Spec (Page No 65) says, you should narrow it after deserializing the handle.
Your j2ee server may be helping you (in terms of reducing one line of code !!! by allowing you to use java casting), but your client code cannot be used as it is if you change your EJB Container.
To clarify more on at what all times we should narrow and cast -- Is it that if the "declared" return type of a method is anything other than the actual remote component interface type (Advice in case of the Advice application from HFEJB) then you always have to narrow and cast - is this right?