Reputation: 351
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?
Upvotes: 1
Views: 186
Reputation: 140328
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.
Upvotes: 7
Reputation: 2953
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.
Upvotes: 1
Reputation: 12391
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();
}
}
Upvotes: 1
Reputation: 2770
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();
}
}
Upvotes: 1