Does it matter when you define the method? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Does it matter when you define the method?

In the example given: class MyClass { static void sayHello() { System.out.println("Hello World!"); } public static void main(String[ ] args) { sayHello(); } } the method is defined and then called. However, in the following quiz slide the method is called and then defined. public static void main(String[ ] args) { hello(); } static void hello() { System.out.println("hi"); } This makes it appear as though the ordering of definition/ calling the method doesn'y matter. Can someone clarify. Thanks.

19th Apr 2019, 4:55 PM
Jeremiah
1 Answer
+ 1
When a method is called the execution jumps to the method block (anywhere in the code - as long as the method is accessible) and starts executing the statements and then jumps back to the place the method was called from. You need to make sure to use appropriate access modifiers for the methods and keep it readable enough though. As to the examples you've presented - both seem correct to me.
19th Apr 2019, 5:43 PM
{ 𝄋 ℒ 𝄋 }
{ 𝄋 ℒ 𝄋 } - avatar