Sunday 15 September 2013

java - Why doesn't try-with-resources work with field variables? -


This is my first question and I'm confused that there is no such question yet!

So the question is:

Why not try resources along with field-variables?

Or in other words: Why do I need a local variable forever?

Here are some example codes:

  Public class FileWriteTest {Public FileWriter file; Public Void workingDemo (try {FileWriter file = new FileWriter} (new file ("someFilePath")} {// do something} Catch (IOException e) {e.printStackTrace ();}} Public Zero withoutWorkingDemo () {file = Try (file = new flumerwriter (new file ("some filepaths")) {// do something} hold (IOException e) {e.printStackTrace ();}}}   

May anyone explain to me why this is a conference?

At any point during execution The example variable can be changed attempted- With K-resources blocks - it can break its unchanging and stop cleaning. Note that local variables are the implications for the same reason.

BTW is a better question, why is Java compels us to declare a local variable, even if we do not see it within the block. For example, C # is not required.

No comments:

Post a Comment