Call without being static? [Question]
I hope this is the right place to post, if not I'm sorry.
I'm new to Java and I've been having trouble calling a non-static method.
I want to call
Code :
public boolean updateSession() {
setActivityState(SessionStates.END_STATE);
return true;
}
from
Code :
public boolean handle(Player player) {
updateSession();
return true;
}
but it wants me to make updateSession() static. Is there anything I can do to get around this?
Re: Call without being static? [Question]
Consider you have a Person class which has a name attribute and a printName method. If that method was static and you called it which name should it print, mine, yours? There arebillions of people on this planet and you need to know which name you want printed. So you create an instance of the Person class to represent each person on the planet. Then the printName method would be non-static. You can then call the printName method of a specific Person object to get their name. I hope that helps.
Re: Call without being static? [Question]
I understand what your saying but I don't understand the definitions well enough to solve it, could you post a short example of what you said please?
Re: Call without being static? [Question]
Basically you need to create an instance of your class so you can call a non-static method.
Code :
Foo myFoo = new Foo(); // creates an instance of the Foo class
myFoo.someMethod(); // calls a non-static method
Foo.anotherMethod(); // calls a static method
Re: Call without being static? [Question]