저는 새 인스턴스를 추가하려고하면 "정적 컨텍스트에서 참조 할 수없는 비 정적 변수"오류를 얻습니다. Edge 클래스 (하위 클래스?)를 내 arraylist에게 보냅니다. 내가 뭘 잘못하고 있는지 알아낼 수 없어!비 정적 변수이 클래스의 인스턴스를 만들 때 정적 컨텍스트에서 참조 할 수 없습니다.
public static class Edge {
...
}
그렇지 않으면, 중첩 된 클래스는 그것의 외부 클래스의 인스턴스에 대한 참조를 유지 즉, 비 정적으로 유지 :
public class stuff{
public static void main(String[] args){
ArrayList<Edge> edges = new ArrayList<Edge>();
edges.add(new Edge(1,2, 3, 4));
}
public class Edge{
private int x1;
private int y1;
private int x2;
private int y2;
private double distance;
private boolean marked;
//constructors
public Edge(int point1_x, int point1_y, int point2_x, int point2_y){
x1 = point1_x;
y1 = point1_y;
x2 = point2_x;
y2 = point2_y;
int x_dist = x1 - x2;
int y_dist = y1 - y2;
distance = Math.hypot((double)x_dist, (double)y_dist);
marked = false;
}
//methods
public void mark(){
marked = true;
}
public boolean isMarked(){
return marked;
}
public double weight(){
return distance;
}
}
}
Edge 클래스는 자체 파일 인 Edge.java에 있어야합니다. –
[정적이 아닌 변수는 정적 컨텍스트 (Java)에서 참조 할 수 없습니다] (http://stackoverflow.com/questions/2559527/non-static-variable-cannot-be-referenced-from-a-static -context-java) – EJP