what is your opinion in this case?# Java - 爪哇娇娃
c*n
1 楼
/* generate a bunch of "random" numbers
taken in serial one by one from a list of numbers in
a given file
*/
class RandomGen(
public RandomGen() throws IOException {
InputStream ios = ..... open "rand.txt" in CLASSPATH;
//
read the file into memory,
}
int idx;
public Integer gen() {
return list_of_numbers[idx++];
}
}
something like the above.
the problem is that the constructor throws IOException, so if I use this
class, and most likely I use it as a static member var:
class MyClass {
static RandomGen gen = new RandomGen();
....
}
then I can't throw in the static initializer.
what is your strategy of handling this?
1) let ctor of RandomGen throw RuntimeException instead?
2) catch the IOException in static initializer of MyClass?
3) ???
Thanks
taken in serial one by one from a list of numbers in
a given file
*/
class RandomGen(
public RandomGen() throws IOException {
InputStream ios = ..... open "rand.txt" in CLASSPATH;
//
read the file into memory,
}
int idx;
public Integer gen() {
return list_of_numbers[idx++];
}
}
something like the above.
the problem is that the constructor throws IOException, so if I use this
class, and most likely I use it as a static member var:
class MyClass {
static RandomGen gen = new RandomGen();
....
}
then I can't throw in the static initializer.
what is your strategy of handling this?
1) let ctor of RandomGen throw RuntimeException instead?
2) catch the IOException in static initializer of MyClass?
3) ???
Thanks