• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

File doubt

 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic