If the tag handler class for MyIterTag implements IterationTag contains a return statement - return Iterationtag.EVAL_BODY_BUFFERED; in it's doStartTag() method, what will be the output?
The answer says that it will not compile because EVAL_BODY_BUFFERED is defined by the BodyTag interface so it's an invalid statement. It can only return SKIP_BODY or EVAL_BODY_INCLUDE.
Ok, I get that. But then the next question:
If the aTagWithBody extends from TagSupport and returns BodyTag.EVAL_BODY_BUFFERED from doStartTag(), what will the above page print?
I said it wouldn't compile, based on the first question, as it seems similar. TagSupport cannot return BodyTag.EVAL_BODY_BUFFERED because it does not implement the BodyTag interface. However, the answer says it will compile but throw an exception at runtime, with the reasoning being "It will compile because the compiler only checks whether the doStartTag() is returning an int or not. It does not check whether the int value is valid or not. However, the tag can be coded to return BodyTag.EVAL_BODY_BUFFERED without an compile time error because the compiler does not have any knowledge of what this tag is allowed to return."
These two answers seem contradictory to me. Maybe I've just looked at these questions too long. I'm going to test this myself, but I would expect that neither one would compile. Can someone explain this?
"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
This is regular Java programming stuff If you try to access a field that is not defined in an interface (i.e. Iterationtag.EVAL_BODY_BUFFERED), it will not compile because Iterationtag.EVAL_BODY_BUFFERED does not exist.
If a method's return type is an int, you can return any int and it will compile. The return value is not invalid from a Java point of view but is invalid from an application logic point of view, which can't be determined by the compiler and so the code fails at run time.
The first example I understand. It's the second one that doesn't make sense. I tried this:
Neither one will compile, so I still don't see how the second example is supposed to compile.
Sure you could do a "return 0" or whatever, but that's not what the question is asking. It's referring to the predefined constants.
Enthuware Software Support
Joined: Sep 23, 2000
That is because you are not trying the second one exactly as specified by the question. The question clearly says that the method returns BodyTag.EVAL_BODY_BUFFERED, while you are trying to return EVAL_BODY_BUFFERED.
But I think I understand your confusion now. You are probably not assuming appropriate import statments and I think the question statement should make it clear that the tag code contains import javax.servlet.jsp.tagext.*;
In the first case, "return EVAL_BODY_BUFFERED;" will not work because IterationTag does not have EVAL_BODY_BUFFERED constant defined. In the second case, it does not depend on IterationTag for the definition of EVAL_BODY_BUFFERED constant because it explicitly returns BodyTag.EVAL_BODY_BUFFERED, which exists.
Just wondering - do you guys know if these questions are still relevant for the current Java 6 version of the exam(1Z0-899)?
Doesn't seem to be any mention of either 'Simple/Custom' tag handlers in the exam objectives.
Any idea if there are questions of this nature on the new exam?