Page 1 of 1

Try with resources

Posted: Sat Dec 16, 2017 7:57 am
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 ?

Re: Try with resources

Posted: Sat Dec 16, 2017 10:16 pm
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.

Re: Try with resources

Posted: Sun Dec 17, 2017 7:26 am
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 ?

Re: Try with resources

Posted: Sun Dec 17, 2017 8:06 am
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.

Re: Try with resources

Posted: Sun Dec 17, 2017 10:19 am
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.

Re: Try with resources

Posted: Sun Dec 17, 2017 12:44 pm
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.

Re: Try with resources

Posted: Tue Jan 09, 2018 12:31 pm
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 ?

Re: Try with resources

Posted: Tue Jan 09, 2018 9:20 pm
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.