Page 1 of 1

About Question enthuware.ocpjp.v7.2.1401 :

Posted: Tue Apr 05, 2016 11:00 am
by doziransky
I vaguely recall a question from an earlier test saying something about how a File object can never be changed once it is created.

So then how can renameTo(File newFile) work?

I'm assuming that it's similar to when you try to change a String, where you don't actually change the String but a new String is created and the reference is reassigned to that String.

Re: About Question enthuware.ocpjp.v7.2.1401 :

Posted: Tue Apr 05, 2016 11:30 pm
by admin
That is correct. Instances of the File class are immutable but its methods can create new ones.
BTW, renameTo is not creating a new File instance. It is just renaming the physical file name. The original File object remains unchanged. You should try it out.
-Paul.