calling another method from the main method in java

kamikaze_pilot :

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.

skaffman :

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling a class from another class with main method

From Dev

Java calling a print method from a main function and using data from another separate method

From Dev

calling method to a main in java

From Java

Calling Java Main method from static block

From Dev

Calling Main Method in Java Class From JSP

From Dev

calling a method from the main to output a phrase in java

From Java

Calling a method from another method

From Java

java calling a method from another class

From Java

Main thread is blocked when calling method from another thread

From Dev

Calling Method of another class from Recursion Method: Java

From Dev

Calling main method with null in Java

From Dev

Calling one method from another method in Python

From Java

Calling method of Another class from run() method

From Dev

Calling an impl method from another impl method

From Javascript

Calling a method from another method in the same class

From Dev

calling an object method from another method not working

From Dev

Calling Test Method from another Test Method

From Dev

Calling Javascript prototype method from another method

From Dev

Exiting from a Method by calling another Method

From Dev

Call Main from another method

From Java

Java cannot find symbol when calling a method from another class

From Java

Having trouble calling a method to edit a string from another class in java

From Dev

calling MainActivty's method from another java class without onCreate

From Dev

Java: Calling variables of user input from one method to another

From Dev

Java LIBGDX, Calling InputHandler's TouchDown method from another class

From Dev

Java - Calling Boolean Method from another class with arraylist parameter

From Dev

Implicitly calling a method in a class from main

From Dev

Printing an Integer and their half calling it from main method

From Dev

Calling a default method from Interface in Main Class

Related Related

  1. 1

    Calling a class from another class with main method

  2. 2

    Java calling a print method from a main function and using data from another separate method

  3. 3

    calling method to a main in java

  4. 4

    Calling Java Main method from static block

  5. 5

    Calling Main Method in Java Class From JSP

  6. 6

    calling a method from the main to output a phrase in java

  7. 7

    Calling a method from another method

  8. 8

    java calling a method from another class

  9. 9

    Main thread is blocked when calling method from another thread

  10. 10

    Calling Method of another class from Recursion Method: Java

  11. 11

    Calling main method with null in Java

  12. 12

    Calling one method from another method in Python

  13. 13

    Calling method of Another class from run() method

  14. 14

    Calling an impl method from another impl method

  15. 15

    Calling a method from another method in the same class

  16. 16

    calling an object method from another method not working

  17. 17

    Calling Test Method from another Test Method

  18. 18

    Calling Javascript prototype method from another method

  19. 19

    Exiting from a Method by calling another Method

  20. 20

    Call Main from another method

  21. 21

    Java cannot find symbol when calling a method from another class

  22. 22

    Having trouble calling a method to edit a string from another class in java

  23. 23

    calling MainActivty's method from another java class without onCreate

  24. 24

    Java: Calling variables of user input from one method to another

  25. 25

    Java LIBGDX, Calling InputHandler's TouchDown method from another class

  26. 26

    Java - Calling Boolean Method from another class with arraylist parameter

  27. 27

    Implicitly calling a method in a class from main

  28. 28

    Printing an Integer and their half calling it from main method

  29. 29

    Calling a default method from Interface in Main Class

HotTag

Archive