简体   繁体   中英

non-static variable reference from a static context (Java)

I've searched for this problem but still not getting any solution.

I declared this simple program:

public class Test{
  public abstract class Person {
      public void talk(){
        System.out.print("I am a person");
      }
      public abstract void display();
  }

  public class Student extends Person {
    public void talk(){
        System.out.println("I am a student");
    }

    public void display(){
        System.out.println("Nice to meet you");
        super.talk();
    }
  }

  public static void main(String args[])
  {
    Student s = new Student();
    s.display();
  }
}

but it keeps giving me the error :

error: non-static variable this cannot be referenced from a static context

    Student s = new Student();

I've always been declaring objects that way! I don't know what's happening today.

I need to understand what am I doing wrong here?

When you declare inner classes:

class Outer {
  class Inner {
  }

  void doSomething() {}
}

there is an implicit reference to an instance of the Outer class held by each instance of Inner . This means that you can write the following in the inner class to refer to the outer instance:

Outer.this.doSomething();

Actually, you can often write simply doSomething() - you only need the Outer.this if the inner class also has a method called doSomething() , and you need to disambiguate.

The long and short of this is that you actually need an instance of Outer to create an instance of Inner :

Outer outer = new Outer();
Inner inner = outer.new Inner();

If you don't actually need to refer to the instance of Outer inside Inner (eg you never need to call doSomething() ), the simplest solution is just to make the inner class static :

class Outer {
  static class Inner {}

  void doSomething();
}

In fact, I would recommend that you always make you inner classes static, unless you really need them to be non-static.

make your Person and Student Classes static

OR

Create Test Object first to create Student object in main method.

Student s = new Test().new Student();

Reason: As Person/Student classes are non static, they can't exist without Test Object. So either those classes should be static or Test() should be created first to create Student.

Remove person and student from your Test class:

public abstract class Person { 
    public void talk(){ 
        System.out.print("I am a person"); 
    }
    public abstract void display();
} 

public class Student extends Person    { 
    public void talk(){ 
        System.out.println("I am a student"); 
    }     
    public void display(){ 
        System.out.println("Nice to meet you");
        super.talk(); 
    } 
}

public class Test{ 
    public static void main(String args[]) { 
        Student s = new Student();    
        s.display(); 
    }
 }

This works all fine:

     abstract class Person {
        public void talk(){
            System.out.print("I am a person");
        }
        public abstract void display();
    }

     class Student extends Person {
         public void talk() {
             System.out.println("I am a student");
         }

         public void display() {
             System.out.println("Nice to meet you");
             super.talk();
         }
     }

    public class Test {


        public static void main(String args[]) {
            Student s = new Student();
            s.display();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM