1

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?

16
  • 1
    Other than the main method, there seem to be no static method, nor the usage of the this keyword. Are you sure you posted the right code? Commented Jan 12, 2016 at 12:49
  • it complains about a variable 'this', which you don't have, you also don't use the keyword this, so, make sure your file is saved, and try again Commented Jan 12, 2016 at 12:49
  • Yes I am. I don't know what am I doing wrong. :S Commented Jan 12, 2016 at 12:50
  • how do you compile and run this programm? Commented Jan 12, 2016 at 12:51
  • I'll create a new project file and try to copy the code there Commented Jan 12, 2016 at 12:51

4 Answers 4

7

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 (e.g. 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.

Sign up to request clarification or add additional context in comments.

2 Comments

Perfect answer @Andy
Inner inner = new Outer().new Inner(); clears everything,,In order to have an instance of inner class you need the instance of outer class
1

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(); 
    }
 }

Comments

1

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();
        }
    }

Comments

1

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.

2 Comments

I don't understand what do you mean by Test object?
Yes I got the idea now. but it's just easier to separate the files. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.