>>> I was wondering why Java behaves this way. . . . Below are a few
>>> lines of code with nearly duplicate methods called for a file and then
[quoted text clipped - 11 lines]
> Alan: is just the name enough, without the whole path, enough to find
> the file to see if it exists?
Mark,
Thank you. If I replace
File file = new File(infile.getName());
with
File file = new File(infile.getPath());
in the "methodWithNew" method, then it works.
So, I guess the bottom line is that I can just set a File object
to the input File object, without needing to use the "new" operator.
When I do that (no "new"), am I just making the new File object in
the method point to the input File object? This sounds OK to me.
Thanks, Alan
Lew - 31 Dec 2007 03:01 GMT
> If I replace
>
[quoted text clipped - 11 lines]
> When I do that (no "new"), am I just making the [not] new File object in
> the method point to the input File object? This sounds OK to me.
You're making the variable 'file' point to the same object as 'infile'.
This sets the variable 'file' to point to the same File object as 'infile',
yes. It is, however, not a "new File object", nor is an object ever made to
point to an object. Objects reference other objects through variables.
File file = infile;
It is the variable 'file' that points to an object, and the variable 'infile'
that points to the same object. There are two variables, pointing to only one
object.
File file = new File( infile.getPath() );
Now the variable 'file' points to a different File object from 'infile'.
There are two variables, pointing to two different objects.

Signature
Lew