Why I have No output as a result? But when I add static to the main and the other method I obtain the desired result | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why I have No output as a result? But when I add static to the main and the other method I obtain the desired result

import java.util.*; public class program{ public void showtab(int[] tab){ for(int i=0;i<tab.length;i++){ System.out.print(tab[i]+" "); } System.out.println(); } public void main (String[] args){ int[] tab = {2,3,1,7,6,9,65,37,10}; System.out.println("your tab before sorting"); showtab(tab); // sorttab(tab); System.out.println("your tab after sorting"); showtab(tab); } }

6th Jan 2020, 5:12 PM
Hichem GOUIA
Hichem GOUIA - avatar
3 Answers
+ 6
You have a class program that has a main method. You cannot alter it's signature, the static is necessary to be added there since main() serves as an entry point to the program. Also like I have mentioned in your other post that only static methods can be accessed inside the main() since it itself is static. So other methods have to be static as well if you want to call them without creating the object of that class.
6th Jan 2020, 5:27 PM
Avinesh
Avinesh - avatar
+ 2
Yes the main() ca be overloaded but one main() with this signature must exist inside the program- public static void main(String[] args) Apart from this you can have as many main() you want with different method signature and yes the static keyword is not necessary for overloaded main()'s.
7th Jan 2020, 12:52 AM
Avinesh
Avinesh - avatar
+ 1
Avinesh well explained 👍 I had a confusion because I found that we can overload the main function... So even if we will overload it we have to keep the static keyword, yes?
6th Jan 2020, 9:59 PM
Hichem GOUIA
Hichem GOUIA - avatar