• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Declare Variable in TRY{}-Statement

 
Thomas Markl
Ranch Hand
Posts: 192
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
please consider code as follows:

Error Message �fileread1.java:10: variable f might not have been initialized; int i = f.read();
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?
Thomas


[This message has been edited by Marilyn deQueiroz (edited July 24, 2001).]
 
PANDORA HL
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure the file named f.txt in your package?
 
Ashish Hareet
Ranch Hand
Posts: 375
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Peace
 
Thomas Markl
Ranch Hand
Posts: 192
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
the file isn't in a package. I don't think this is the problem
for the Compiler.
Thomas
 
Thomas Markl
Ranch Hand
Posts: 192
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please see http://www.javaranch.com/ubb/Forum24/HTML/007928.html:
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.
Regards
------------------
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 = f.read();
)
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic