• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File doubt

 
sun par
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Kem Bro
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
.
 
sun par
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sun par
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic