This site requires JavaScript, please enable it in your browser!
Greenfoot back
Reaper47
Reaper47 wrote ...

2016/6/26

'non-static method cannot be referenced from a static context'

Reaper47 Reaper47

2016/6/26

#
I'm trying to call an method in one class from another, and when I try to do this I get this error. 'non-static method takeDamage(double) cannot be referenced from a static context' I don't even know what static method necessarily means in Greenfoot so I have no idea on how I would go about fixing the context/method. Any help on the issue or just a brief explanation on what 'static' means in Greenfoot would be great!
danpost danpost

2016/6/26

#
The use of 'static' is not something that is limited entirely to greenfoot-- it is a part of the java language itself (see Class members in the java tutorials). The message is saying that you are trying to call a method without supplying any object (of the class the method is in) to execute the method on. You are supplying a class name, which does not point to any particular object, but to the class itself. All 'public static' members of a class can be accessed by using dot notation of the name of a class followed by the member name. However, the method or field (member) you are trying to access is not 'static' -- it needs an object of the class to execute on or for. In other words, the class is not what is taking damage, but some object that is created from the class, which you will need an object reference to. For detailed help on this, you will need to supply the code you are trying to use with a brief explanation of what it is supposed to be doing. Supply class names for where the code given is in and where the method being called (takeDamage) is in.
Reaper47 Reaper47

2016/6/26

#
Thank you for explaining how that all works! I just figured out the problem, I just needed to add 'import java.util.List;' Completely forgot about it haha. Everything is working as intended!
You need to login to post a reply.