-3

I tried to run these codes but Netbean kept showing up non-static variable this can not be referenced from static context. I will appreciate your answer if some guys someone can show me why. Thanks!

Code here:

abstract class Bike {

    abstract void Run();
}

class Honda extends Bike {

    void Run() {
        System.out.println("Running Safely");
    }
}

public static void main(String arg[]) {

    Bike obj = new Honda();
    obj.Run();

  }

}
Abdul Fatir
  • 6,159
  • 5
  • 31
  • 58
  • 6
    The static always reoccurring problem with new users is that they absolutely never do prior research. You are a beginner with a new language; just guess: how many people where at the same point, and asked the same question before? See here http://www.tutorial4us.com/java/java-static-and-non-static-variable for example. – GhostCat Jun 17 '16 at 07:54
  • I would suggest you also to read this article (http://stackoverflow.com/questions/12690128/how-to-instantiate-non-static-inner-class-within-a-static-method) – wake-0 Jun 17 '16 at 08:12

1 Answers1

0

See, "this" keyword refers to current object due to which method is under excecution. As, you cannot call static method using instance of class. That is why, "this" can't be used in the above example in a static method as it is trying to print current instance which is not at all created. So, I think thats why there is an compile time error that you are getting.

Gagan Chouhan
  • 312
  • 1
  • 6