Also, why does the code in the examples keep appearing with the main on the bottom? Isnt it supposed to go right after class? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Also, why does the code in the examples keep appearing with the main on the bottom? Isnt it supposed to go right after class?

4th Jul 2020, 7:32 PM
Don
3 Answers
+ 3
You can put the main method anywhere inside the class. After all these are just methods and could be declared in any order. The only thing that matters is that they must be inside a class.
4th Jul 2020, 7:56 PM
Avinesh
Avinesh - avatar
+ 1
Awesome!!!! That makes sense!!!! Thanks a bunch, Avinesh!!!!!
4th Jul 2020, 8:08 PM
Don
0
Im doin the java part. I see that the second block of code has the main. I flipped both blocks and i still get the same result, and i dont have any problems with following the examples, i'm just curious if theres a "proper" way or if it doesnt matter. Like i said it prints out the same value both ways. Thanks in advance! class MyClass { static int sum(int val1, int val2) { return val1 + val2; } public static void main(String[ ] args) { int x = sum(2, 5); System.out.println(x); } } //or this way class MyClass { public static void main(String[ ] args) { int x = sum(2, 5); System.out.println(x); } static int sum(int val1, int val2) { return val1 + val2; } }
4th Jul 2020, 7:53 PM
Don