reusing File variable

A

angelochen960

Hi,

Is this the correct way of reusing the file variable?

File f = new File("file1");
f.delete();

f = new File("file2");
f.delete;

thanks
 
J

John B. Matthews

Is this the correct way of reusing the file variable?

It might be clearer to say "reusing a reference to an instance of the
class File."
File f = new File("file1");

At this point, f contains a reference to an instance of the class
File having the name "file1".
f.delete();

f = new File("file2");

At this point, f contains a reference to a different instance of the
class File having the name "file2". The original instance of File is
no longer accessible, unless it is saved elsewhere in the program.
f.delete;

Instances of the class File are immutable and can't be reused, as
"the pathname represented by a File object will never change."

<http://java.sun.com/javase/6/docs/api/java/io/File.html>

See also,

<http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.3>
 
L

Lew

Is this the correct way of reusing the file [sic] variable?

Spelling counts. In particular, case counts.
It might be clearer to say "reusing a reference to an instance of the
class File."

But he isn't reusing a reference, he's reusing a variable.
At this point, f contains a reference to an instance of the class
File having the name "file1".


At this point, f contains a reference to a different instance of the
class File having the name "file2". The original instance of File is
no longer accessible, unless it is saved elsewhere in the program.


Instances of the class File are immutable and can't be reused, as
"the pathname represented by a File object will never change."

<http://java.sun.com/javase/6/docs/api/java/io/File.html>

See also,

<http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.3>

But he isn't reusing an instance of class 'File'. He's reusing a variable to
point to a new instance of class 'File.'

To the OP: What you did is correct and valid, assuming correct consideration
of appropriate scope.

Variables should be declared for the minimum scope in which they're useful.
Within that scope, it's perfectly valid to reuse them to point to different
instances. However, if the variable scope is too wide, you should declare a
new one each time in a narrower scope instead. For example:

WRONG (usually):
File f;
for ( String name : names )
{
f = new File( name );
// ...
}

RIGHT (usually):
for ( String name : names )
{
File f = new File( name );
// ...
}
 
J

John B. Matthews

[QUOTE="Lew said:
Is this the correct way of reusing the file [sic] variable?

Spelling counts. In particular, case counts.
It might be clearer to say "reusing a reference to an instance of the
class File."

But he isn't reusing a reference, he's reusing a variable.[/QUOTE]

Ah, thank you for clarifying this. The OP is reusing a variable, named f, \
that holds a reference to an instance of the class File.

[...]

But he isn't reusing an instance of class 'File'. He's reusing a
variable to point to a new instance of class 'File.'

Would this have been more apropos?

<http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.12.2>

[...]
 
A

angelochen960

RIGHT (usually):

Thanks for all the replies, actually the code is to test if a file has
been created, first the file is deleted then the object in test is
called to generate a file and test again if the file is generated, two
File variables are used, I was thinking if there is a way to use only
one:

@Test
public void test() {
String xmlFileName = "test.xml";

File f = new File(xmlFileName);
f.delete(); // delete before test

// call the class to create a xml file

File f2 = new File(xmlFileName);
Assert.assertTrue(f2.exists(), "xml file not found");

}
 
L

Lew

... the code is to test if a file has
been created, first the file is deleted then the object in test is
called to generate a file and test again if the file is generated, two
File variables are used, I was thinking if there is a way to use only
one:

Sure. Just use one. Try it.
@Test
public void test() {
String xmlFileName = "test.xml";

File f = new File(xmlFileName);
f.delete(); // delete before test

// call the class to create a xml file

You don't call a class, you call a method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top