Try with resources

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
horst1a
Posts: 37
Joined: Mon Jun 12, 2017 2:16 am
Contact:

Try with resources

Post by horst1a »

Hello,

I have always thought, when you use a TWR you must declare type of resource in the brackets , too,
auch as:
try
{ A a = new A();} // more code

Now , I have seen code where this is not true, it even compiles, such as:

private A a;
try
{ a = new A();} // more code

However , I found that code ´snippet in a constructor, not in the main-method.
Is there difference ?

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

Re: Try with resources

Post by admin »

It not clear from your code what you mean.
I don't see any resource being "declared in brackets" in the first code snippet that you have posted.

-Paul.
If you like our products and services, please help us by posting your review here.

horst1a
Posts: 37
Joined: Mon Jun 12, 2017 2:16 am
Contact:

Re: Try with resources

Post by horst1a »

You are right , i forgot the brackets. should be this way.
Try
{ ( A a = new A();)
}
this works , assuming A is a class.

When I do this
private A a,
Try
{ ( a = new A();)
}

i get a compiler error.

But i saw code working fine like the above, when the TWR was placed in a constructor.
Where is the diffefrence ?

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

Re: Try with resources

Post by admin »

I still don't see any differemce between the two code snippets that you've posted. Also, please post the complete and exact code that you are saying compiles.
The code that you have posted will not compile.
If you like our products and services, please help us by posting your review here.

horst1a
Posts: 37
Joined: Mon Jun 12, 2017 2:16 am
Contact:

Re: Try with resources

Post by horst1a »

I need to know the following:
When i use a TWR-block, do i have to declare the type or ressource inside the TWR-block such as

try
{
( A a = new A();)
}

or can it even be declared outside the TWR-block suh as

private A a;
try
{
( a = new A();)
}
THe difference is that in the 2nd example i dont declare A as A a =new A() in the TWR-block,as i had declared the A already as a private member before the try-statement.
I think this should not compile, but i am sure i saw code in relation to getConnection(), when the resource Connection con was declared outside the TWR-block.

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

Re: Try with resources

Post by admin »

If you want a resource to be closed automatically, then you have to declare it it within TWR. The syntax for that is:
try(A a = new A()) {
}

Try compiling the following and see what happens:
A a;
try(a = new A()) {
}


What you have written, i.e.
try {
(A a = new A());
}
is an incorrect syntax altogether and so will not compile.

As I said before, you need to actually write and try to compile the code to understand correctly.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

horst1a
Posts: 37
Joined: Mon Jun 12, 2017 2:16 am
Contact:

Re: Try with resources

Post by horst1a »

Im still unsure about it.

When i have the following code snippet

Code: Select all

class A
try(A a = new A()) {
}
I must declare the A a within the try - block or i get a compiler error.

But in the following code

Code: Select all

class DBConnect
private Statement stmt;
private Connection con:

try
{
con = DriverManager.getConnection(anything)
stmt = con.createStatemnt();

}

i declare con and stmt OUTSIDE the try - block and dont get not compiler error.

Where is the diference ?

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

Re: Try with resources

Post by admin »

As I said before, you need to show the complete and exact code that you are trying to run. Without that it is very difficult to figure out what you are trying to do.

In your post, you have written just class A. What is it? Does it implement AutoCloseable? If you want to use it in try(A a = new A), it has to implement AutoCloseable. Are you aware of that? Have you read any priliminary tutorial on this topic such as https://docs.oracle.com/javase/8/docs/t ... urces.html ? Have you read any book? Which one?

Please remember that posting questions on a forum cannot be a substitute for a book.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 22 guests