Re: New File with a Directory



Alan wrote:
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 [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.

--
Lew
.