What I’m simply trying to do is extending a class from LinkedList. Here is my code:
import java.util.*;
class Test {
public static void main( String [] args ) {
OrderedLinkedList ol = new OrderedLinkedList();
}
public class OrderedLinkedList extends LinkedList<Integer> {
public boolean add( Integer item ) {
for (int i=0; i < size(); i++) {
Integer itemOfList = get( i );
if ( itemOfList.compareTo( item ) > 0 ) {
add( i, item );
break;
}
}
return true;
}
}
}
However, I got compile error with this message:
Test.java:7: non-static variable this cannot be referenced from a static context
OrderedLinkedList ol = new OrderedLinkedList();
^
1 error
I believe I’m not referencing anything but instantiating.
An inner class has an implicit reference to its outer class, unless marked static.
You need to mark the inner class static to avoid this:
So the compiler is complaining that there isn’t an instance of Test for the inner class as main is a static method.