I'm having trouble in my main
method. On the line MyInt r1 = new MyInt(2);
I get a compiler error with the message: "No enclosing instance of type ex4 is accessible".
public class ex4 {
public class MyInt implements Comparable<MyInt> {
private int value;
MyInt(int x){ this.value= x;}
public String toString() {
return ("Result: " + intValue());
}
public int intValue() { return value; }
public int compareTo(MyInt rhs){
if ( value >rhs.value ){
return 1;}
if (value < rhs.value){
return -1;}
else return 0;
}
}
public static void main(String[] args) {
MyInt r1 = new MyInt(2); //Error here ("No enclosing instance of type ex4 is accessible. Must qualify the allocation with an enclosing instance of type ex4 (e.g. x.new A() where x is an instance of ex4)."
MyInt r2 = new MyInt(7); // same here
System.out.println(r1.compareTo(r2));
}
}
I've seen other responses to similar questions but I'm unable to solve my own issue, could anyone help? I'm trying to compare one value to another. Thanks.