-2

This program aims to create a GUI component which displays the amount of total space, available space and percentage of total space that is available:

package com.java24hours;

import java.io.IOException;
import java.nio.file.*;
import javax.swing.*;

public class FreeSpacePanel extends JPanel {
    JLabel spaceLabel = new JLabel("Disk space: ");
    JLabel space = new JLabel();

    public FreeSpacePanel() {
        super();
        add(spaceLabel);
        add(space);
        try {
            setValue();
        } catch (IOException ioe) {
            space.setText("Error");
        }
    }

    private final void setValue() throws IOException {
        // get the current file storage pool
        Path current = Paths.get("");
        FileStore store = Files.getFileStore(current);
        // find the free storage space
        long totalSpace = store.getTotalSpace();
        long freeSpace = store.getUsableSpace();
        // get this as a percentage (with two digits)
        double percent = (double)freeSpace / (double)totalSpace * 100;
        percent = (int)(percent * 100) / (double)100;
        // set the label's text
        space.setText(freeSpace + " free out of " + totalSpace + " ("
            + percent + "%)");
    }
}

You can see a FileStore object is created called 'store', then in the following lines the FileStore methods getTotalSpace() and getUseableSpace() are called directly without implementation. However, the FileStore class declares these methods abstract, so how is this possible?

Boann
  • 48,794
  • 16
  • 117
  • 146
ABC123
  • 123
  • 6
  • Instead of including line numbers, please just use in-code comments to note certain lines. The prefix line numbers make your code very difficult to run if necessary. – Carcigenicate Jun 22 '19 at 18:10
  • 2
    Because all that matters is that the variable type is `FileStore`, which has those methods. The `Files.getFileStore` method returns a concrete implementation of the abstract `FileStore` class. Also see [What does it mean to program to a interface?](https://stackoverflow.com/questions/1413543/what-does-it-mean-to-program-to-a-interface). – Slaw Jun 22 '19 at 18:18

1 Answers1

3

Files.getFileStore returns an instance of some non-abstract subclass of FileStore that implements the required methods.

To see what class it is, do:

System.out.println(store.getClass());

On my Linux system I see the object is an instance of class sun.nio.fs.LinuxFileStore.

Boann
  • 48,794
  • 16
  • 117
  • 146
  • `sun.nio.fs.WindowsFileStore` on Windows [:-) – user85421 Jun 22 '19 at 18:49
  • I cant seem to find the class in the java API, why is this? – ABC123 Jun 22 '19 at 19:06
  • @ABC123 It's a private implementation detail, not public API. But [it exists](https://hg.openjdk.java.net/jdk9/jdk9/jdk/file/tip/src/java.base/windows/classes/sun/nio/fs/WindowsFileStore.java). – Boann Jun 22 '19 at 19:08
  • So is this java class in the java api class files on my computer? If so why isn't it public? – ABC123 Jun 22 '19 at 19:09
  • @ABC123 Well it has to be on your computer if it's running. There are tons of classes in the `sun.*` packages, which support the Java implementation, and which are used by classes in `java.*` and `javax.*`, but which are not part of the public specification. They are implementation details. – Boann Jun 22 '19 at 19:14
  • So are all implementation classes are part of the sun.* packages or are there other packages which contain these implementation classes? – ABC123 Jun 22 '19 at 19:57
  • Also why keep these classes hidden? – ABC123 Jun 22 '19 at 19:57
  • 1
    @ABC123 They could be anywhere. For example, the method [`Collections.singletonList`](https://docs.oracle.com/javase/9/docs/api/java/util/Collections.html#singletonList-T-) doesn't say what `List` class it returns. Currently, it happens to return an instance of the private class [`java.util.Collections.SingletonList`](https://hg.openjdk.java.net/jdk9/jdk9/jdk/file/65464a307408/src/java.base/share/classes/java/util/Collections.java#l4800), defined just below the method. That file has lots of similar private classes. As for why they are hidden, I already said: they are implementation details. – Boann Jun 22 '19 at 20:12
  • Keeping these classes hidden is good program design. It prevents users from depending on details that might change, be different from system to system, or just shouldn't be cared about. – Louis Wasserman Jun 23 '19 at 02:45
  • they are hidden since they should not be used directly - one use for abstract classes or interfaces. This way classes can be changed (e.g. new Java version or a different *provider* of Java) without affecting existing code. In this particular case, normally the Linux classes like `LinuxFileStore` are not present on Windows; as the Windows one not on Linux systems – user85421 Jun 23 '19 at 06:21