Page 1 of 1

About Question enthuware.ocpjp.v7.2.1373 :

Posted: Sat Aug 01, 2015 4:06 pm
by gk_javauser
Hello,

I need clarifiaction on this question:

Originally, stmt is null, then it is assigned to a statement derived from the connection opened in the try block. After the connection is closed, can't stmt be assigned in a different try-catch block with a different connection? I would try this out on my own, but I don' have jdbc setup on my system.

Is stmt = null considered to be a reference only, and not an object, since it is null?

Thank you!

Re: About Question enthuware.ocpjp.v7.2.1373 :

Posted: Sat Aug 01, 2015 8:45 pm
by admin
Yes, the variable stmt is in scope after the try block and can be assigned another Statement object. But the question is referring to the objects of the code snippet that is given in the question.

HTH,
Paul.

Re: About Question enthuware.ocpjp.v7.2.1373 :

Posted: Fri May 20, 2016 7:17 pm
by codecodecode67
I get the explanation of the answer and I've also answered correctly, however, I find the wording of the question a bit confusing.

Scenario:
In the case that we use a regular try-catch instead of a try-with-resources, so we don't close any of the closeable objects, what would be the right answer to this question?

Thanks

Re: About Question enthuware.ocpjp.v7.2.1373 :

Posted: Fri May 20, 2016 9:54 pm
by admin
I don't see any confusion. The question has given some code and that code has try with resources block. It is asking about the objects that can be used right after that block.

You can use all the objects that are in scope at that point. To find out which objects are in scope after the try block, I would suggest you to try it out.

-Paul.

Re: About Question enthuware.ocpjp.v7.2.1373 :

Posted: Tue Jan 14, 2025 3:01 am
by cosmind
I also found this question confusing. Didn't know what to understand by "used to query database". I was thinking if it can be used with a different connection, or if results can be fetched from it. Especially that it seems to test the scopes of resources. Statement stmt is still in scope after the the code snippet.

Re: About Question enthuware.ocpjp.v7.2.1373 :

Posted: Tue Jan 14, 2025 11:52 pm
by admin
It is talking about the same objects and not the same variables. The stmt variable is still in scope but the connection object has been closed, so the same Statement object pointed to by stmt cannot be used after the try block.

Since it is causing confusion, we will improve the wording to make it more clear.
thank you for your feedback!