I'm combining existing import and export functions in order to reduce the number of times the user gets told the connection was refused in the event that the connection does get refused. The library I'm calling has separate import and export functions, as well as a combined import/export function. The export function requires the list of files to be exported, while the combined function figures out the list by itself (and internally calls the export function with this list). Since either import or export can be turned off by the user, I want to only get the list of files if necessary.
I have come up with this code:
List<File> files; if (mExport) files = ListFiles(); if (mExport && mImport && files.size() > 0) // Error is on files DoExportAndImport(); else if (mImport) DoImport(); else if (mExport && files.size() > 0) // No error here DoExport(files);
if statement gets marked with the error
Variable 'files' might not have been initialized (but not the last one).
Help me with my reasoning here: if
mExport is true, then
files gets initialized; but if
mExport is false, then the second
if statement gets short-circuited and never reaches
files, so it doesn't matter that
files hasn't been initialized, because it isn't being used.
Am I overlooking something here, or is this just too complex a situation for the compiler to work out? If the latter, is there any way to tell the compiler to knock it off, I've got it handled?
For the record, initializing
files = new ArrayList<>() in the declaration does silence the compiler, as does
files = null, though that obviously leads to other errors; but it just feels like such a waste to initialize a value that I know is never going to be used or referred to in any way.