Computer Science Canada Error: non-static method XX cannot be referenced from a static context |
Author: | juggernaut117 [ Sun Apr 11, 2010 7:16 pm ] |
Post subject: | Error: non-static method XX cannot be referenced from a static context |
This is probably a very common problem among new programmers like me, I've searched everywhere but I can't find a definitive answer, so I decided to post here. First of all here's my main method Quote: public static void main(String [] args)
{ String modeSelect,returnTitle; do { //Below here, the methods welcome() and doublePlay() are non-static, which is my main problem modeSelect = welcome(); doublePlay(); System.out.println("You have chosen the 2 player mode in which you will face off against each other"); System.out.println("Would you like to return to title? (Y/N)"); Scanner get= new Scanner(System.in); returnTitle = get.nextLine(); } while (returnTitle == "Y" || returnTitle == "y"); } I won't post the other methods because I personally don't think it makes a difference. When I tried compiling this, the 2 errors are Quote: non-static method welcome() cannot be referenced from a static context
non-static method doublePlay() cannot be referenced from a static context Everything else is fine, so what seems to be the problem? I'll post the rest of the code if it's needed, thanks in advance |
Author: | TerranceN [ Sun Apr 11, 2010 7:35 pm ] |
Post subject: | RE:Error: non-static method XX cannot be referenced from a static context |
A class is a blueprint of an object, it contains methods and variables that belong to the class itself, such as your main method. You let java know this with the static keyword. It also contains methods and variables that belong to each object created from the class (which is commonly called an instance). What you are trying to do is call a method that would belong to an instance, without an instance, which gives you an error. In order to access those methods from your main method you would either have to make those other methods static (it's what makes more sense here), or create a new object from your class and call the methods using that (not recommended because non-static methods are meant for methods that change non-static variables). Hope that helps. |
Author: | juggernaut117 [ Sun Apr 11, 2010 8:09 pm ] |
Post subject: | RE:Error: non-static method XX cannot be referenced from a static context |
Making those methods won't be a good idea since within those methods there're even more non-static methods that uses non-static variables, so simply changing method welcome() and doublePlay() to static will result in a chain reaction causing all the other variables and methods to stop working. |
Author: | TerranceN [ Sun Apr 11, 2010 8:28 pm ] | ||
Post subject: | Re: Error: non-static method XX cannot be referenced from a static context | ||
You could create a variable in main that is created from your class, then call the methods using that variable. Something like this, where ClassName is the name of your Class and object can be any possible variable name:
That should work, but the whole point of using instances is for mass creation of similar objects (or to use inherited properites, but that is more advanced stuff). Its always possible to code using objects but it is best to not use them until you need them and understand them. If you do not need objects why not make everything else static? |
Author: | juggernaut117 [ Sun Apr 11, 2010 8:37 pm ] |
Post subject: | RE:Error: non-static method XX cannot be referenced from a static context |
awesome, it works now, thank you very much |
Author: | TheGuardian001 [ Sun Apr 11, 2010 8:37 pm ] | ||
Post subject: | Re: Error: non-static method XX cannot be referenced from a static context | ||
The methods can stay the way they are. You just need to access them through an object. So instead of calling them directly from main, do something like this:
In that, instead of simply calling the method as a member of the Class MyClass, you treat it as a member of the Object mc, which is of type MyClass. Or you could declare everything as static. But honestly, I find creating an object easier. |