Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Error: non-static method XX cannot be referenced from a static context
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
juggernaut117




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
TerranceN




PostPosted: 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.
juggernaut117




PostPosted: 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.
TerranceN




PostPosted: 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:

Java:
ClassName object = new ClassName();
modeSelect = object.welcome()
object.doublePlay();


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?
juggernaut117




PostPosted: 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
TheGuardian001




PostPosted: 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:

Java:

public class MyClass
{
    //Constructor method. Used to create the object.
    public MyClass()
    {
        //Do stuff
    }
    public static void main(String[] args)
    {
         //Creates a new Object of type MyClass. This object
         //Has access to all the non-static methods and variables
         //In the class.
         MyClass mc = new MyClass();

         //Call our non-static method.
         mc.myMethod();
     }

     //Oh no! a non static method!
     //Can't call this one directly
     public void myMethod()
     {
          //do stuff
     }
}


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.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: