0

In Java, does it matter whether a method is defined first or called first?

In the methods tutorial for Java it begins with this code: class MyClass { static void sayHello() { System.out.println("Hello World!"); } public static void main(String[ ] args) { sayHello(); } } // Outputs "Hello World!" It seems as though the method [which they say is System.out.println("Hello World!")] is defined first, and then called with "sayHello();" Am I thinking of that right? But then, later a later example says this: public static void main(String[ ] args) { int res = max(7, 42); System.out.println(res); //42 } static int max(int a, int b) { if(a > b) { return a; } else { return b; } } In this example it seems like the "max()" method is called first by the main, and then defined. So my question is does it matter whether the method is defined or called first? Also am I correct in thinking the main is what calls the method?

8th Dec 2018, 4:11 PM
Joshua Stebbins
Joshua Stebbins - avatar
1 Respuesta
+ 21
● It doesn't matter whether U define method after or before main method or some other method [order in which methods are defined doesn't matter] ● what matters is the order of method calls made in main method .[remember main method is starting point of java program]
8th Dec 2018, 4:32 PM
Gaurav Agrawal
Gaurav Agrawal - avatar