Reputation: 653
From an optimization stand point, is it better to declare the File
separately like this
File f = new File("sample.txt");
FileReader fr = new FileReader(f);
or, is it better to do it inline like this
FileReader fr = new FileReader(new File("sample.txt));]
Aren't really sure if it even matters really.
Upvotes: 1
Views: 211
Reputation: 25950
If you are going to reference the just-created instance new File("sample.txt)
later in your code, then File f = new File("sample.txt");
would be required. You would be able to access it via the reference variable f
.
Upvotes: 2
Reputation: 1
The difference is obviously the file object couldn't be easily accessed in the code in inline version. And it makes the code less readable, maintainable, and debuggable.
Upvotes: 1
Reputation: 33534
- Whether you create a Object Reference Variable
of type File
to have a reference to the File
object or not, it will still be present on the heap
.
- Yes its quite valid that having a Object Reference Variable
will help you refer back to that File
object when you need it the next time....
Upvotes: 1
Reputation: 1500175
It makes no difference. Do whatever's more readable in your particular situation.
It's possible that it could affect when the File
object is eligible for garbage collection, but I'd be hugely surprised to see a situation in which that's a significant difference.
Upvotes: 4