Detailed Java Generics

J

jsc59

I have a very detailed/complicated java generics question. Consider
the following code

public abstract class AEdge {
ANode fr, to;
}

public abstract class ANode { }

public class CEdge extends AEdge { }

public class CNode extends ANode { }

public class MyEdgeDatabase<N extends ANode, E extends AEdge> {
Set<E> edges;

public abstract class ForEachEdgeFrom {
public ForEachEdgeFrom(N n) {
for(E e : edges) if (e.fr == n) run(e);
}
public abstract void run(E e);
}
}


public class Test {
MyEdgeDatabase<CNode, CEdge> edgeDB
= new MyEdgeDatabase<CNode, CEdge>();
CNode n = new CNode();

public void doIt() {
edgeDB.new ForEachEdgeFrom(n){
public void run(CEdge e) {
System.out.println(e);
}
};
}
}

The doIt() method will not compile. It gives the following error:

"No enclosing instance of type MyEdgeDatabase<N,E> is accessible. Must
qualify the allocation with an enclosing instance of type
MyEdgeDatabase<N,E> (e.g. x.new A() where x is an instance of
MyEdgeDatabase<N,E>)."


What does this mean and how to I fix it? Please don't suggest other
ways of iterating through edges in MyEdgeDatabase (we have reason to do
it this way).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top