• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compile time errors

 
Venkat Ramsimha
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Animal
{
public void eat()
{
}
public void printYourself()
{
System.out.println("in animal");
}
}
public class Horse767 extends Animal
{
public static void main(String args[])
{
printYourself();
}
public void printYourself()
{
super.printYourself(); // Invoke the superclass
}
}


hi all,
the above program is giving compile time errors.can anybody provide the explanation for the above as why its giving compile time errors

thanks
venkat
 
Ashok Kumar
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error reads as follows:

non-static method printYourself() cannot be referenced from a static context

Which is self explanatory
 
Raghu Shree
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
main() method is a static method method. we can't acess non static methods and non static variables within static methods. so compiler shows the error message. Just change your code like new Animal().printYourself().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic