27

I have

class foo{

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

   public void do(){}


}

but then when I call do() from main by running the command java foo on the command line, java complains that you can't call a method from a static function.

So my question is: How do you call methods from the main method and if it is not possible what are some alternative strategies to call methods after the program is run from the command line using the java command.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
kamikaze_pilot
  • 14,304
  • 35
  • 111
  • 171
  • 7
    The word "do" is a keyword, you can't name a method like this. Stick with (at least the very basic) conventions, name classes starting with a capital letter. – maaartinus Jan 31 '11 at 08:25

6 Answers6

51

You can only call instance method like do() (which is an illegal method name, incidentally) against an instance of the class:

public static void main(String[] args){
  new Foo().doSomething();
}

public void doSomething(){}

Alternatively, make doSomething() static as well, if that works for your design.

skaffman
  • 398,947
  • 96
  • 818
  • 769
  • 3
    Do not make it static, it's no alternative. It works, but there should be a good reason for making anything static. – maaartinus Jan 31 '11 at 08:24
  • 2
    @maaartinus: You can make that kind of sweeping statement when you nothing about what the code does? – skaffman Jan 31 '11 at 08:26
  • 1
    Can't I? Sure, without knowing more, I can't. However, what's the chance that the only method called from main should be static? There are so few legitimate reasons for static methods, that using them is nearly never a good idea. Especially for newbies. – maaartinus Jan 31 '11 at 08:44
15

Check out for the static before the main method, this declares the method as a class method, which means it needs no instance to be called. So as you are going to call a non static method, Java complains because you are trying to call a so called "instance method", which, of course needs an instance first ;)

If you want a better understanding about classes and instances, create a new class with instance and class methods, create a object in your main loop and call the methods!

 class Foo{

    public static void main(String[] args){
       Bar myInstance = new Bar();
       myInstance.do(); // works!
       Bar.do(); // doesn't work!

       Bar.doSomethingStatic(); // works!
    }
 }

class Bar{

   public do() {
   // do something
   }

   public static doSomethingStatic(){
   }
}

Also remember, classes in Java should start with an uppercase letter.

Shmencer
  • 3
  • 2
yan.kun
  • 6,820
  • 2
  • 29
  • 38
9

This is a fundamental understanding in Java, but can be a little tricky to new programmers. Do a little research on the difference between a static and instance method. The basic difference is the instance method do() is only accessible to a instance of the class foo.

You must instantiate (create an instance of) the class, creating an object, that you use to call the instance method.

I have included your example with a couple comments and example.

public class SomeName {

//this is a static method and cannot call an instance method without a object
public static void main(String[] args){

    // can't do this from this static method, no object reference
    // someMethod();

    //create instance of object
    SomeName thisObj = new SomeName();
    //call instance method using object
    thisObj.someMethod();
}

//instance method
public void someMethod(){
    System.out.print("some message...");
}

}// end class SomeName
jamesTheProgrammer
  • 1,747
  • 4
  • 22
  • 34
8

You can do it multiple ways. Here are two. Cheers!

package learningjava;

public class helloworld {
    public static void main(String[] args) {
        new helloworld().go();
        // OR
        helloworld.get();
    }

    public void go(){
        System.out.println("Hello World");
    }
    public static void get(){
        System.out.println("Hello World, Again");
    }
}
Muniko
  • 93
  • 1
  • 5
1

If you want to use do() in your main method there are 2 choices because one is static but other (do()) not

  1. Create new instance and invoke do() like new Foo().do();
  2. make static do() method

Have a look at this sun tutorial

0

First java will not allow you to have do() method. Instead you can make it doOperation().

Second You cann't invoke directly non static methods from static function. Main is a static function. You need to instantiate the class first and then invoke method using that instance.

Third you can invoke static method directly from non static methods.

Aliaksei Kliuchnikau
  • 13,589
  • 4
  • 59
  • 72
csarathe
  • 420
  • 1
  • 5
  • 12