$ javac TestFilter.java
TestFilter.java:19: non-static variable this cannot be referenced from a static context
for(File f : file.listFiles(this.filterFiles)){
^
1 error
$ sed -i 's@this@TestFilter@g' TestFilter.java
$ javac TestFilter.java
$ java TestFilter
file1
file2
file3
TestFilter.java
import java.io.*;
import java.util.*;
public class TestFilter {
private static final FileFilter filterFiles;
// STATIC!
static{
filterFiles = new FileFilter() {
// Not Static below. When static, an error:
// "accept(java.io.File) in cannot implement
// accept(java.io.File) in java.io.FileFilter;
// overriding method is static"
//
// I tried to solve by the change the problem at the bottom.
public boolean accept(File file) {
return file.isFile();
}
};
}
// STATIC!
public static void main(String[] args){
HashSet<File> files = new HashSet<File>();
File file = new File(".");
// IT DID NOT WORK WITH "This" but with "TestFilter".
// Why do I get the error with "This" but not with "TestFilter"?
for(File f : file.listFiles(TestFilter.filterFiles)){
System.out.println(f.getName());
files.add(f);
}
}
}
Update: define “current object”
Constructor created, object created but the this does not refer to the current object “test”. It works when I change this to “test” but it does not work with “this”. Why?
$ javac TestFilter.java
TestFilter.java:28: non-static variable this cannot be referenced from a static context
for(File f : this.getFiles()){
^
1 error
$ cat TestFilter.java
import java.io.*;
import java.util.*;
public class TestFilter {
private static final FileFilter filterFiles;
private HashSet<File> files;
static{
filterFiles = new FileFilter() {
public boolean accept(File file) {
return file.isFile();
}
};
}
TestFilter(){
files = new HashSet<File>();
File file = new File(".");
for(File f : file.listFiles(filterFiles)){
files.add(f);
}
}
public static void main(String[] args){
// CONSTRUCTOR with no pars invoked and object "test" created here!
TestFilter test = new TestFilter();
// Why does it not work with "this"?
// "test" is surely the current object.
for(File f : this.getFiles()){
System.out.println(f.getName());
}
}
public HashSet<File> getFiles() { return files; }
}
thisis used to refer to “instance” attributes or method ( among others ). Instance means a new object exist and each object ( instance ) have a copy of the given attribute.The
class name( in your caseTestFilter) is used to refer to “class” attributes or methods ( those who do not require an instance to extist.So, in your first line you’re declaring
filterFilesas a class attribute ( you don’t require an instance for that.See:
This means, you declare class attribute named:
filterFilesof typeFileFilterwhich isprivateand whose reference can’t be changed ( because it isfinal).Since it is a class attribute you may access it in the
mainmethod ( which is a class level method ). This both will work:and
But
Won’t, because
thisrefers to the current instance, but since you’re in a class level method ( main ) there is no instance, so, there is nothisor in compiler words: non-static variable this cannot be referenced from a static contextInstance attributes are unique per instance. Class level attribute are unique per class.
Consider the following class:
I hope this sample make everything clear.