Question

I'm beginner in Java and I have a basic question about main class and main method.I try to create method such as addition under the main method . throw the error like "non-static method". what is reason ? thanks...

Was it helpful?

Solution

I guess you using a code like this.

public class TestClass {

public static void main(String[] args) {
    doSth();
}

public void doSth() {

}

You cannot call a non-static method from the main class. If you want to call a non-static method from your main class, instance your class like this:

TestClass test = new TestClass();
test.doSth();

and call the method.

OTHER TIPS

A static method means that you don't need to invoke the method on an instance(Object). A non-static (instance) method requires that you invoke it on an instance. So think about it: if I have a method changeThisItemToTheColorBlue() and I try to run it from the main method, what instance would it change? It doesn't know. You can run an instance method on an instance, like someItem.changeThisItemToTheColorBlue().

More information at http://en.wikipedia.org/wiki/Method_(computer_programming)#Static_methods

The only way to call a non-static method from a static method is to have an instance of the class containing the non-static method. By definition, a non-static method is one that is called ON an instance of some class, whereas a static method belongs to the class itself.

Is like when you try to invoke the non-static method startsWith of class String without an instance:

 String.startsWith("Hello");

What you need is to have an instance and then invoke the non-static method:

 String greeting = new String("Hello World");
 greeting.startsWith("Hello"); // returns true 

So you need to create and instance to invoke it.

And to be more clear about Static methods you can refer

https://softwareengineering.stackexchange.com/questions/211137/why-can-static-methods-only-use-static-data

You defined your method without the keyword 'static' I think. You cannot call a non-static method in a static context such as the main method.

See Java Object Oriented Programming

The main method is a static method, so it does not exist inside an object.

To call non-static methods (methods without the "static" keyword in front of their definitions), you need to create an object of the class, using new.

You can just make the other method static, that will fix the immediate problem. But it may or may not be good Object Oriented design to do this. It would depend on what you were trying to do.

You can't call a non-static method from a static method without instantiation of the class. If you'd like to call another method without creating a new instance (a new object) of the main class you have to use the static keyword for the another method also.

    package maintestjava;

    public class Test {

      // static main method - this is the entry point
      public static void main(String[] args)
      {
          System.out.println(Test.addition(10, 10));
      }

      // static method - can be called without instantiation
      public static int addition(int i, int j)
      {
        return i + j;
      }
    }

If you would like to call non static methods you have to instatiate the class, this way creating a new instance, an object of the class:

package maintestjava;

public class Test {

  // static main method - this is the entry point
  public static void main(String[] args)
  {
      Test instance = new Test();
      System.out.println(instance.addition(10, 10));
  }

  // public method - can be called with instantiation on a created object
  public int addition(int i, int j)
  {
    return i + j;
  }
}

See more: Static keyword on wikipedia Static on about.com

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top