This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Declare Variable in TRY{}-Statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Declare Variable in TRY{}-Statement" Watch "Declare Variable in TRY{}-Statement" New topic

Declare Variable in TRY{}-Statement

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
please consider code as follows:

Error Message � variable f might not have been initialized; int i =;
I think this error is not because of scope as variable f is
defined outside the try{}-blocks.
In my oppinion the reason is a CONDITIONAL INITIALIZATION which
is not allowed in Java.
If read() reports an exeption f might not be initialized because
JVM leaves try block to catch-block.
What ist your oppinion?

[This message has been edited by Marilyn deQueiroz (edited July 24, 2001).]

Joined: Jul 23, 2001
Posts: 5
Are you sure the file named f.txt in your package?
Ashish Hareet
Ranch Hand

Joined: Jul 14, 2001
Posts: 375
Here's my say on this one -
The scope of f is fine , the only problem being that it is an automatic variable so no initialization on the class loaders part for this one . The workaround would be
FileInputStream f = null ;//right upon declaration or
f = null ;//in the first catch block or
static FileInputStream f ;/*right before your main , the class loader will initialize it for you , but this would be too restrictive & dangerous aswell .*/
And regarding
Originally posted by Thomas Markl:
In my oppinion the reason is a CONDITIONAL INITIALIZATION which
is not allowed in Java.

I'm new to Java , i have no clue about what that meant
Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
the file isn't in a package. I don't think this is the problem
for the Compiler.
Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Please see
posted February 06, 2001 02:36 PM
Hello Venkat!
I guess you know you have to initialize all local/automatic variables, if you don't you will get a compiler error. But in some cases it is allowed to initilize them inside a nested block. The reason for why it doesn't work in your example is that it is inside a try block, and this means that the variable f might not get initialized. Because if an exception is thrown before f is initialized, the try block will be abandoned imediately and execution will continue in the first suitable catch block. This would leave f without a value and therefore the compiler does not permit this.
Dominic Steng�rd
Sun Certified Java 2 Programmer
[This message has been edited by Dominic Steng�rd (edited February 06, 2001).]
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

I think there are two ways around this.

The link someone else posted didn't work for me, so apologies if I'm repeating something.

You could quite easily initialize the variable to null when you declare it. This way the compiler won't complain that it might not be initialized.

The other thing is to enclose both of your lines of code
f = new FileInputStream(""); // and
int i =;
in the same try block, and use two different catch blocks, one for the FileNotFoundException and a second one for the IOException. Because the FileNotFound one is a subclass of IO, you would need to attempt to catch it first, and catch the IO one second.
subject: Declare Variable in TRY{}-Statement
It's not a secret anymore!