This question already has answers here:
Closed 5 years ago.
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.
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.
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.
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
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");
}
}
If you want to use do()
in your main method there are 2 choices because one is static but other (do()) not
- Create new instance and invoke do() like
new Foo().do();
- make
static do()
method
Have a look at this sun tutorial
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.