Search code examples
javafileiofinal

Can I make a File final if I want to write to it later?


Is it okay to make an object of type File, which I later would like to write into, have a final declaration? I'd like to pass it to a Runnable inner class.


Solution

  • final declaration allows you to write/read into, but not change the reference of the object.

    final File fileRef = new File("/path/to/my/file.txt");
    fileRef = new File("/new/path/to/file"); // illegal