0

Google says to call any non-static method or variable in a static context, you need to construct the object first. This is exactly what I did here but I am still getting an error:

public class Example
{
 
public static void main(String args[]){
    One one = new Two("Lovelace");
    one.methodA();
    one.methodB();
}

public class One
{
    public One(){
        System.out.println("Turing");
    }
    
    public void methodA(){
        System.out.println("zuse");
    }
    
    public void methodB(){
        methodA();
    }
}

public class Two extends One {
    public  Two(String s){
        System.out.println(s);
    }
    
    public void methodA(){
        System.out.println("Von Neumann");
    }
}

I am getting an error on One one = new Two("Lovelace"); it says non static variable cannot be referenced from a static context but I literally created a new object so whats happening?

rhian
  • 23
  • 5
  • 1
    You have nested classes where they have references back to the parent class but you're instantiating them without a parent. Make the nested classes static. Or put all classes at same level in file so none are nested. – Nathan Hughes May 04 '22 at 23:28
  • Oh I didn't think nested classes would make difference @NathanHughes – rhian May 04 '22 at 23:44

0 Answers0