Java:静态-非静态-这个问题
$ 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);
}
}
}
更新:定义“当前对象”
创建构造函数,创建对象,但 this
不引用当前对象< /em>“测试”。当我将其更改为“test”时它可以工作,但它不适用于“this”。为什么?
$ 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; }
}
$ 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; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
关键字
this
引用当前的对象——您没有的东西,因为您的方法是静态的。这意味着它在类本身上运行,而不是在任何对象上运行,因此任何this
的使用都是无效的 - 即使您尝试访问的特定变量是静态的也。访问静态成员的正确方法是通过类:TestFilter.filterFiles
,而不是this.filterFiles
。The keyword
this
refers to the current object -- something that you don't have, because your method is static. That means it's running on the class itself, not on any object, so any use ofthis
is invalid -- even though the specific variable you're trying to access is static too. The correct way to access a static member is by the class:TestFilter.filterFiles
, notthis.filterFiles
.this
用于引用“实例”属性或方法(除其他外) 。实例意味着存在一个新对象,并且每个对象(实例)都有给定属性的副本。类名
(在您的例子中是TestFilter
)用于引用“类”属性或方法(那些不需要实例存在的属性或方法。因此,在在第一行中,您将
filterFiles
声明为类属性(您不需要为此提供实例。请参阅:
这意味着,您声明名为:
class 的属性>filterFiles
类型为FileFilter
,它是private
并且其引用不能更改(因为它是final
)。是一个 class 属性,您可以在
main
方法中访问它(这是一个类级别的方法)。这两者都可以工作:但
不会
,因为
this
指的是当前实例,但由于您处于类级方法 ( main ) 中,因此没有实例,因此,没有this
或编译器的话:非静态变量 this 不能从静态上下文引用实例属性对于每个实例都是唯一的。类级别属性对于每个类都是唯一的。
考虑以下课程:
我希望这个示例能让一切变得清晰。
this
is 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
filterFiles
as a class attribute ( you don't require an instance for that.See:
This means, you declare class attribute named:
filterFiles
of typeFileFilter
which isprivate
and whose reference can't be changed ( because it isfinal
).Since it is a class attribute you may access it in the
main
method ( which is a class level method ). This both will work:and
But
Won't, because
this
refers to the current instance, but since you're in a class level method ( main ) there is no instance, so, there is nothis
or 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.
您不能在静态函数中引用实例变量,例如“this”指针。由于 TestFilter 是类,并且 filterFiles 变量是静态的,因此它可以工作,因为您可以在静态函数中使用静态变量。
如果 TestFilter 应该实例化为类,我建议将 main 函数内的代码移至构造函数。在这种情况下,您将能够访问“this”。
you cannot reference instance variables like the "this" pointer in static functions. Since TestFilter is the class and the filterFiles variable is static, it works, because you can use static vars in static functions.
If TestFilter is something that is supposed to be instantiated as a class, I suggest moving the code inside the main function to the constructor. In that case, you would be able to access "this".
这是对实际使用的对象实例的引用。在静态方法中,该类未实例化 - 这在此上下文中没有任何意义。
this is a reference to the instance of the object actually used. In a static method the class wasn't instantiated - this has no meaning in this context.
静态成员由类引用,而不是由实例引用。这意味着您必须使用类名来引用静态成员,而不是实例名。
因为
this
引用一个实例,所以您会收到编译错误。Static members are referenced by Class, not by instance. What this means is that you must use class name to reference the static member, not instance name.
Because
this
refers to an instance, you get compile error.