- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Why we can't initialize static final variable in try/catch block in java?
In Java you can declare three types of variables namely, instance variables, static variables and, local variables.
- Local variables − Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.
- Class (static) variables − Class variables are variables declared within a class, outside any method, with the static keyword.
Static methods in try-block
In the same way, static variables belong to the class and can be accessed anywhere within the class, which contradicts with the definition of the local variable. Therefore, declaring a static variable inside a method, block or, constructor is not allowed. Still, if you try to declare a static variable within a block a compile time error will be generated.
Example
In the following Java example we are trying to declare a String variable path in the try block.
import java.io.File; import java.io.FileInputStream; public class Example { public static void main(String args[]){ System.out.println("Hello"); try{ static String path = "my_file"; File file =new File(path); FileInputStream fis = new FileInputStream(file); }catch(Exception e){ System.out.println("Given file path is not found"); } } }
Compile time error
on compiling, the above program generates the following error.
Example.java:7: error: illegal start of expression static String path = "my_file"; ^ 1 error
If you compile the same program in eclipse it generates the following message.
- Related Articles
- Can we declare a try catch block within another try catch block in Java?
- Can we initialize blank final variable in Java
- Can we have a try block without a catch block in Java?\n
- Can we define a try block with multiple catch blocks in Java?
- Can a try block have multiple catch blocks in Java?
- Can we write code in try/catch block in JSP as well?
- Why Java wouldn't allow initialization of static final variable in a constructor?
- Can we to override a catch block in java?
- Can we have an empty catch block in Java?
- Explain Try/Catch/Finally block in PowerShell
- Why interfaces don't have static initialization block when it can have static methods alone in java?
- Can we initialize static variables in a default constructor in Java?
- Why can't we define a static method in a Java interface?
- What is static blank final variable in Java?
- Why final variable doesn't require initialization in main method in java?

Advertisements