R
Robert
I have a class which needs to take a string as input in its
constructor. I want to parse the string into constant member
variables, but this does not appear to work due to the way exception
handling is interpreted. Essentially, we have:
public class Foo {
public Foo(String input) {
try {
p = Integer.parseInt(input);
} catch(Exception e) {
p = 1;
}
}
private final int p;
}
This fails because the compiler thinks p might already be assigned:
[javac] Compiling 3 source files to C:\pig\build\classes
[javac] C:\pig\src\Foo.java:6: variable p might already have been
assigned
[javac] p = 1;
[javac] ^
[javac] 1 error
Is there another way to do this, or will I have to content myself with
not declaring p as final? I suppose I could always make a tempP and
assigned p that, but I would hope there is a more elegant way to do
this.
constructor. I want to parse the string into constant member
variables, but this does not appear to work due to the way exception
handling is interpreted. Essentially, we have:
public class Foo {
public Foo(String input) {
try {
p = Integer.parseInt(input);
} catch(Exception e) {
p = 1;
}
}
private final int p;
}
This fails because the compiler thinks p might already be assigned:
[javac] Compiling 3 source files to C:\pig\build\classes
[javac] C:\pig\src\Foo.java:6: variable p might already have been
assigned
[javac] p = 1;
[javac] ^
[javac] 1 error
Is there another way to do this, or will I have to content myself with
not declaring p as final? I suppose I could always make a tempP and
assigned p that, but I would hope there is a more elegant way to do
this.