3
File file = new File ("image.gif");

// Is this a copy, a reference, or something else?
File File2 = file;

What I'm trying to do is copy a reference to a File object. I don't want to make a duplicate and I'm not sure if the above code is copying a reference or copying the object. I'm trying to avoid memory leaks or orphans, especially if I were to delete the file from within the code.

Thank you.

user3621633
  • 1,681
  • 3
  • 32
  • 46
  • 3
    What do you think a `File` object is or represents? Why do you think so? – Sotirios Delimanolis Aug 05 '15 at 15:34
  • An object that is a reference to a file on disk? File is the class which facilitates this. – user3621633 Aug 05 '15 at 15:36
  • 2
    It is not a reference to a file on disk. It is a java class, that stores the path to real file on the disk, and provides you with methods to manipulate that file, but it does not reference it for sure. – darijan Aug 05 '15 at 15:37
  • 3
    In 2015 you shouldn't be looking at `File` but [`Path`](http://docs.oracle.com/javase/8/docs/api/java/nio/file/Path.html) instead – fge Aug 05 '15 at 15:44
  • @fge You're right. But for Java 6, I don't believe `Path` works. I still work with Java 5 and 6 sometimes. – user3621633 Aug 05 '15 at 16:47

3 Answers3

3

What you just did is: you created another variable File2 that points to the exactly same object on the heap. A new object is not created!

If you would manipulate any of the variables, you would manipulate the same object on the heap. A new one is not created in the process. A new object is created only with the new keyword (or with some Reflection, but that's out of the scope now).


Also, make sure to read about where are variables created, where do objects reside during program's runtime and what are heap and stack (search for: "java heap vs stack").

darijan
  • 9,725
  • 25
  • 38
3

A File object is

An abstract representation of file and directory pathnames.

It's just a fancy String (with methods that do interact with file systems).

file is a variable. A variable holds values. Since this is a reference type variable, it holds reference values.

new File ("image.gif"); is an expression that creates a new object and produces a reference value (referencing the newly created object).

File2 is another variable. When you perform the assignment

File File2 = file;

the value stored in file is copied and the copy is stored in File2.

This is the same concept as binding argument values to method parameters, discussed in:

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • This is really, really great stuff. Thank you very much. I'll be sure to take a look at that pass-by-reference or pass-by-value stackoverflow link. – user3621633 Aug 05 '15 at 15:45
  • 2
    @user3621633 I also recommend [Pass-by-Value Please (Cup Size continued)](http://www.javaranch.com/campfire/StoryPassBy.jsp) (and its first part - [Cup Size -- a story about variables](http://www.javaranch.com/campfire/StoryCups.jsp)) – Pshemo Aug 05 '15 at 16:00
  • Fantastic. Thank you. I'll take a look at those links. I read through that original link in the answer. It's similar to C or C++, the pass-by-value vs pass-by-reference, right? In Java, you (almost?) never point to a pointer (reference). You're pointing to a value and there could be multiple pointers pointing to the same value. – user3621633 Aug 05 '15 at 16:44
1

To copy a file object:

File f = new File("home/users", "alice/documents");
File copied = new File(f.getPath());

System.out.println(copied.equals(f)); // true
System.out.println(copied == f);      // false

Usually, this is not necessary. But one case it may be useful is when you want a constructor from File when you subclass java.io.File:

public class MyFile extends File {
    public MyFile(File path) {
        super(path.getPath());
    }

    // ...
Matthias Ronge
  • 9,403
  • 7
  • 47
  • 63