• Post Reply Bookmark Topic Watch Topic
  • New Topic

Non-Static variable this cannot be referenced from static content?  RSS feed

 
Manda Putra
Greenhorn
Posts: 10
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, its heavy rain there... so cold. But code always warming you up.
I want to use @override in my program. This is the output that i want



But there is some mistake make and i dint know what is mean, a non static variable and static context in java.
its say Non-Static variable this cannot be referenced from static content in



And this is my code :



so what should i change to get that output? and what is static and non static in java?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should move your Goat class so that it is outside the Animal class. Right now it is inside the Animal class, therefore it becomes a nested inner class of Animal.  See https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html for more information.

Your code is like this right now:
Again, this makes the Goat class a nested (non-static) class of Animal.  On line 8 above, you try to reference the Goat class. However, line 8 is in a static method.  You cannot attempt to access something that is non-static directly from the static context of your main method, hence, the compile-time error.

To fix this quickly, move the Goat class outside your Animal class:
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manda Putra wrote:
And this is my code :

Even if you fix the code like I told you and you can get your program to work, the way you use inheritance mechanisms and calling super.Name() etc from the subclass is NOT the way inheritance is meant to be used. I don't have a good explanation for you right now; I'll have to think of a way to explain without writing an entire essay on inheritance. Maybe it would be better if you just try to look for more examples of how inheritance is used. Just know that the way you are doing it there is NOT the way it's supposed to be done, even if it seems to work fine.

Another thing: method names should start with a lowercase letter, so your Name(), Eat(), and Species() methods should be name(),eat(), and species(). Those are just the changes you need to make to conform to the standard naming conventions. There are other changes you could make but that should probably wait until you are more familiar with how to properly define and use classes, fields, and inheritance/overriding.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:. . . Right now it is inside the Animal class, therefore it becomes a nested inner class of Animal. . . .
The most likely reason for that mistake is that you have been indenting your code incorrectly. You think you have reached the end of the class and the compiler thinks you haven't. I think you shou‍ld change the way you edit the code. Look at the suggestions in this old post of mine, including the suggestions about text editors and the link about writing backwards.
There is no such things as @override. It is called @Override.
 
Manda Putra
Greenhorn
Posts: 10
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all,  
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!