Hello, I made a simple console program which has a main method and another method I'll call testMethod. If I call the method from inside main I get a compiler error about the testMethod not being static. If I make the method static all is well. I did not create the executing program using the new command but I would believe the object for the class is created when the program executes.
Please alleviate my confusion.
You are best to read Sun's Java tutorial from the start. An instance of your class is created if you used the new keyword to create it. You can then call the instance methods on that instance. If you didn't create an instance in your code, only the class is loaded and that's why you can call the static methods (class methods).
what r0*x means is, if you try to call methods in other classes or even the same class as:
then those methods must be static, however nonstatic methods or also known as class member functions need to be created instances of them. eg.
There are various advantages and disadvantages to using static methods, as you will read in the tutorials.
public class StaticVsNot
public static void main(String args)
//first call to static method no instance is created.
// create an instance of the class to call it's member functions.
StaticVsNot svn = new StaticVsNot();
public static void staticWelcome()
public void nonStaticWelcome()
You will have already used this using the Scanner class for reading input. Scanner classes methods are not static which is why you need to create an instance and to use dot notation.
Thanks, I understand now.