
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Effectively final variables in try-with-resources in Java 9?
Any variable that has been used within Try with Resource statements requires to be declared within the Try statement up to Java 8 version. Since Java 9, this restriction has been removed, and any final or effectively final variables have been used inside the Try block. Effectively final means that the variable can't be changed once it has been initialized.
Example
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; public class EffectivelyFinalTest { private static File file = new File("try_resources.txt"); public static void main(String args[]) throws IOException { file.createNewFile(); BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); try(bufferedReader) { System.out.println("Can Use Final or Effectively Final in Try with Resources!"); } finally { System.out.println("In finally block"); } } }
Output
Can Use Final or Effectively Final in Try with Resources! In finally block
- Related Articles
- How to declare multiple resources in a try-with-resources statement in Java 9?
- What are the improvements for try-with-resources in Java 9?\n
- Try-with-resources in Kotlin
- final variables in Java
- How to use a final or effectively final variable in lambda expression in Java?
- Final static variables in Java
- How to use try-with-resources with JDBC?
- Why variables are declared final in Java
- Difference between constants and final variables in Java?
- Final variables in C#
- What is blank final variable? What are Static blank final variables in Java?
- Assigning values to static final variables in java\n
- Can we declare final variables without initialization in java?
- Static and non static blank final variables in Java
- What happens if we try to extend a final class in java?

Advertisements