8- The try-with-resources Statement

Jun 18, 2026 04:36 · 2:26 · English · Whisper Turbo · 2 Hökmdarlar
Bu transkriptin müddəti bitdi 21 günlər. Daimi saxlama üçün yüksəlt →
Yalnız göstər
0:03
S… Speaker 1 (8- The try-with-resources Statement)
In this video I'm going to show you a better way to release external resources
0:07
S… Speaker 1 (8- The try-with-resources Statement)
like file handles and database connections.
0:10
S… Speaker 1 (8- The try-with-resources Statement)
First I'm going to delete this ugly finally block,
0:13
S… Speaker 1 (8- The try-with-resources Statement)
that's better.
0:14
S… Speaker 2 (8- The try-with-resources Statement)
Now,
0:16
S… Speaker 1 (8- The try-with-resources Statement)
in Java we can declare and initialize our external resources as
0:21
S… Speaker 1 (8- The try-with-resources Statement)
part of the try statement.
0:22
S… Speaker 2 (8- The try-with-resources Statement)
So
0:24
S… Speaker 1 (8- The try-with-resources Statement)
right after the try keyboard and before the left brace,
0:27
S… Speaker 1 (8- The try-with-resources Statement)
we type a pair of parentheses and inside this parentheses
0:31
S… Speaker 1 (8- The try-with-resources Statement)
we create and initialize our external resources.
0:34
S… Speaker 1 (8- The try-with-resources Statement)
So here we can type var reader equals new file
0:38
S… Speaker 1 (8- The try-with-resources Statement)
reader. Now,
0:41
S… Speaker 1 (8- The try-with-resources Statement)
we delete this line over here and this
0:46
S… Speaker 1 (8- The try-with-resources Statement)
variable declaration over here.
0:47
S… Speaker 1 (8- The try-with-resources Statement)
So this is what we call the try with resources
0:51
S… Speaker 1 (8- The try-with-resources Statement)
statement.
0:52
S… Speaker 1 (8- The try-with-resources Statement)
A try statement that has one or more resource.
0:55
S… Speaker 1 (8- The try-with-resources Statement)
With this structure,
0:56
S… Speaker 1 (8- The try-with-resources Statement)
we don't need to explicitly close this resource inside the
1:00
S… Speaker 1 (8- The try-with-resources Statement)
final block.
1:01
S… Speaker 1 (8- The try-with-resources Statement)
The Java compiler will do that for us.
1:03
S… Speaker 1 (8- The try-with-resources Statement)
So when we compile this code,
1:05
S… Speaker 1 (8- The try-with-resources Statement)
the Java compiler will translate this code to what we had in
1:09
S… Speaker 1 (8- The try-with-resources Statement)
the last video.
1:10
S… Speaker 1 (8- The try-with-resources Statement)
It will add a final block and explicitly close the reader
1:14
S… Speaker 1 (8- The try-with-resources Statement)
object.
1:14
S… Speaker 2 (8- The try-with-resources Statement)
Now,
1:15
S… Speaker 1 (8- The try-with-resources Statement)
for this to work,
1:16
S… Speaker 1 (8- The try-with-resources Statement)
this class should implement the auto -closable interface.
1:20
S… Speaker 1 (8- The try-with-resources Statement)
Let me show you.
1:20
S… Speaker 2 (8- The try-with-resources Statement)
So,
1:22
S… Speaker 1 (8- The try-with-resources Statement)
Here's the documentation for the file reader class.
1:25
S… Speaker 1 (8- The try-with-resources Statement)
Look at the interfaces implemented by this class.
1:28
S… Speaker 1 (8- The try-with-resources Statement)
One of them is auto -closable.
1:30
S… Speaker 1 (8- The try-with-resources Statement)
This interface declares a method
1:35
S… Speaker 1 (8- The try-with-resources Statement)
called close.
1:36
S… Speaker 1 (8- The try-with-resources Statement)
So the close method that we have in the file reader actually comes
1:40
S… Speaker 1 (8- The try-with-resources Statement)
from here.
1:40
S… Speaker 1 (8- The try-with-resources Statement)
So the objects that we create over here should implement
1:44
S… Speaker 1 (8- The try-with-resources Statement)
the auto -closable interface.
1:46
S… Speaker 1 (8- The try-with-resources Statement)
Now we can create multiple objects here.
1:48
S… Speaker 1 (8- The try-with-resources Statement)
Let me show you.
1:49
S… Speaker 1 (8- The try-with-resources Statement)
First I'm going to put this on a new line.
1:52
S… Speaker 1 (8- The try-with-resources Statement)
Now we terminate this statement with a semicolon,
1:55
S… Speaker 1 (8- The try-with-resources Statement)
and then we can create another object.
1:58
S… Speaker 1 (8- The try-with-resources Statement)
New file writer,
2:01
S… Speaker 1 (8- The try-with-resources Statement)
some file.
2:02
S… Speaker 1 (8- The try-with-resources Statement)
Now pay attention to how I have formatted this code.
2:06
S… Speaker 1 (8- The try-with-resources Statement)
I'm following the same convention we have in Java.
2:09
S… Speaker 1 (8- The try-with-resources Statement)
So I put the left bracket right after the
2:13
S… Speaker 1 (8- The try-with-resources Statement)
try keyword,
2:14
S… Speaker 1 (8- The try-with-resources Statement)
and the right bracket over here.
2:16
S… Speaker 1 (8- The try-with-resources Statement)
So this is how we can use the try with resources statement.
2:20
S… Speaker 1 (8- The try-with-resources Statement)
Next I'm going to show you how to throw exceptions in Java.

Bu transkript AI (avtomatik səs tanıması) tərəfindən yaradılmışdır. Xətalar ola bilər - istifadə etmək üçün orijinal səslə müqayisə edin. AI siyasəti

❤️ Sevgi STT.ai? Dostlarınıza deyin!
İcmal
Bu transkriptin AI qısa mə'lumatını yaratmaq üçün Summarize düyməsini basın.
İcmallaşdırılır...
Bu transkript haqqında AI-dən soruş
Bu transkript haqqında hər şeyi soruşun — AI uyğun hissələri tapacaq və cavab verəcək.