K
kookey
Today i always get this error:
non-static variable xxx cannot be referenced from a static context
^
e.g.
import java.net.URL;
class Test{
public static void main(String[] args){
URL url = this.getClass().getResource("1.xml");
System.out.println(url);
}
}
error: non-static variable this cannot be referenced from a static
context ^
But,when
import java.net.URL;
class Test{
public Test(){
URL url = this.getClass().getResource("1.xml");
System.out.println(url);
}
public static void main(String[] args){
Test test = new Test();
}
}
The result is right.
I want why.
non-static variable xxx cannot be referenced from a static context
^
e.g.
import java.net.URL;
class Test{
public static void main(String[] args){
URL url = this.getClass().getResource("1.xml");
System.out.println(url);
}
}
error: non-static variable this cannot be referenced from a static
context ^
But,when
import java.net.URL;
class Test{
public Test(){
URL url = this.getClass().getResource("1.xml");
System.out.println(url);
}
public static void main(String[] args){
Test test = new Test();
}
}
The result is right.
I want why.