Forums Register Login

File doubt

+Pie Number of slices to send: Send
Instances of the File class are immutable; that is, once created, the abstract pathname represented by a File object will never change
-------------------------------------------------
This is what the API says...
But when I run a program with lines like these
------------------------------------------------
File f3=f.createTempFile("suni",null);
f3=f.createTempFile("suni",null,new File("."));
-------------------------------------------------
it does not complain... Can anyone please explain then what immutable means?
Thanks
+Pie Number of slices to send: Send
Hi Sunita,
It's probably easier to explain the concept of how some objects are immutable by using this simple example.
If I create a string like this:
String s = "Hello";
I can *never* change this String. If I do something
like:
s = s.concat("World");
A new string is created and assigned to the reference s, the original String is not changed.
AFAIK other objects that are immutable are the wrapper classes for primitive objects.
Cheers
.
+Pie Number of slices to send: Send
Hai Kem,
Thanks.. But we create file objects using one of the constructors... So we will always be able to assign something else to it.. So what is the significance of the instances of the file class being immutable?
Thanks
+Pie Number of slices to send: Send
Let's look at this another way:
File f3=File.createTempFile("suni",null);
File f4=File.createTempFile("suni",null,new File("."));
Clearly we have created 2 separate File objects. In your example you have also created two separate File objects but you only saved a pointer to one of them.
+Pie Number of slices to send: Send
Thanks Thomas ... File class does not have methods to change the abstract path name like the String class.. so I guess that way it stays imuttable
I love a woman who dresses in stainless steel ... and carries tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 646 times.
Similar Threads
I got 94%
Servlet orginal API in Eclipse
a question about File class(urgent)
immutable
DataSource creation in WSAD 5
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 02:53:06.