Final member trouble  
Author Message
Ryan Stewart





PostPosted: 2004-7-21 8:52:00 Top

java-programmer, Final member trouble import java.awt.Image;
import javax.imageio.ImageIO;
import java.io.*;

public class FinalTryCatch {

final Image someImage;

public FinalTryCatch() {
try {
someImage = ImageIO.read(new File("someImageFile.gif"));
} catch (IOException ioe) {
someImage = null;
}
}

public static void main(String[] args) {
FinalTryCatch f = new FinalTryCatch();
// Do some stuff
}
}

This fails to compile with "variable someImage might already have been
assigned" on "someImage = null;". Remove that line and it complains,
"variable someImage might not have been initialized". Obviously. What would
you do in this situation? Make the variable non-final? Have the constructor
throw IOException? Something else?


 
David Hilsee





PostPosted: 2004-7-21 9:22:00 Top

java-programmer >> Final member trouble
"Ryan Stewart" <email***@***.com> wrote in message
news:email***@***.com...
> import java.awt.Image;
> import javax.imageio.ImageIO;
> import java.io.*;
>
> public class FinalTryCatch {
>
> final Image someImage;
>
> public FinalTryCatch() {
> try {
> someImage = ImageIO.read(new File("someImageFile.gif"));
> } catch (IOException ioe) {
> someImage = null;
> }
> }
>
> public static void main(String[] args) {
> FinalTryCatch f = new FinalTryCatch();
> // Do some stuff
> }
> }
>
> This fails to compile with "variable someImage might already have been
> assigned" on "someImage = null;". Remove that line and it complains,
> "variable someImage might not have been initialized". Obviously. What
would
> you do in this situation? Make the variable non-final? Have the
constructor
> throw IOException? Something else?

The easiest and most readable solution, IMHO is:

final Image someImage = getImage();

private Image getImage() {
try {
return ImageIO.read(new File("someImageFile.gif"));
} catch (IOException ioe) {
return null;
}
}

--
David Hilsee


 
Murray





PostPosted: 2004-7-21 9:24:00 Top

java-programmer >> Final member trouble
"Ryan Stewart" <email***@***.com> wrote in message
news:email***@***.com...
> import java.awt.Image;
> import javax.imageio.ImageIO;
> import java.io.*;
>
> public class FinalTryCatch {
>
> final Image someImage;
>
> public FinalTryCatch() {
> try {
> someImage = ImageIO.read(new File("someImageFile.gif"));
> } catch (IOException ioe) {
> someImage = null;
> }
> }
>
> public static void main(String[] args) {
> FinalTryCatch f = new FinalTryCatch();
> // Do some stuff
> }
> }
>
> This fails to compile with "variable someImage might already have been
> assigned" on "someImage = null;". Remove that line and it complains,
> "variable someImage might not have been initialized". Obviously. What
would
> you do in this situation? Make the variable non-final? Have the
constructor
> throw IOException? Something else?

If it's not required to be final, then by all means remove the final
modifier. Otherwise, could you do something like this?

public FinalTryCatch() {
Image testImage;
try {
testImage = ImageIO.read(new File("someImageFile.gif"));
} catch (IOException ioe) {
testImage = null;
}
someImage = testImage;
}