About Question enthuware.ocpjp.v7.2.1373 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
gk_javauser
Posts: 9
Joined: Sun Jul 19, 2015 6:31 pm
Contact:

About Question enthuware.ocpjp.v7.2.1373 :

Post 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!

admin
Site Admin
Posts: 10382
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.

codecodecode67
Posts: 14
Joined: Sun Dec 06, 2015 2:15 pm
Contact:

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

Post 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

admin
Site Admin
Posts: 10382
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.

cosmind
Posts: 2
Joined: Sun Sep 29, 2024 9:10 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 10382
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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!

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests