I talked about the main method a few times in my last article, and itās apparent in a lot of my code snippet, and for this reason I think itās a befitting bonus material for this series.
now let's take a dive right straight into it.
Like I said, a bonus material, I want to simplify the main() in java in the best way I possibly can at the moment, and so, this is intended to be a post mortem on main methods based off my experience using it.
The Main Method().
Can you explain the main method?
There are some important points to note about the main methods
(i.) Consider;
Can you tell if the above code is going to compile or not?
Yes the code will compile fine with no errors, now whether the class contains a āmain()ā or not, and whether or not it is declared according to requirements, these things wouldnāt be checked by the compiler. The code was compiled from command prompt, so if it appears complex, do not fret, I will share a link that teaches the installation of the ide I use, INTELLIJ IDEA and also another link to teach you how to use command prompt or terminal in this case:
command prompt for java tutorial link and IntelliJ Java IDE setup link.
Thatās why the code compiles fine.
Now to attempt to run the code:
At runtime, JVM is responsible for checking whether a āclassā contains a āmain()ā or not and whether it is properly declared or not.
In this case, JVM is unable to find a main() in the class, and thatās why at runtime, we will get the error below:
In other words, whether a class contains a main method or not, and whether the main method is declared according to requirement or not, these things wonāt be checked by the compiler, because itās not its duty, instead at runtime, JVM is responsible for checking them.
If JVM is unable to find a main method, then we will get the runtime exception as shown above, subsequently, JVM will in turn suggest for the required prototype of main method.
What is this prototype now?
(ii.) Observe:
Main() should compulsorily be;
Now at runtime JVM, when JVM suggests for a main(String[]) method, this is the above main() only.
Here questions may arise like:
ā¢ Why does JVM suggest for this prototype only?
ā¢ Can I change it or not?
ā¢ Why is the name of the main() always main, and what if I donāt want to use main, perhaps I would prefer to change the name to āhighmain()ā, is it going to work or not?
OF COURSE, NOT!
Hereās why; the JVM is also a software program;
And within it, if youāre trying to execute a Java class, the prototype/method that is declared is:
The above is the signature prototype that is configured by default and that is why JVM will always suggest for the above method only.
Now it is possible to change this main method to another name, say, ālumainā method, but youāll have to change it inside the program also, that is, to customize the behavior of the JVM, and hence the main method is changeable, consequentially it will now suggest only the ālumainā method prototype only.
JVM is not fixed, existing JVM can call for only main, and my customized JVM will call for ālumainā as the main method.
So whenever youāre asked, is it possible to change the name of the main(), yes it is, but customization of the JVM is required.
The question that ideally follows is, āhow can you customize?ā
Well, I am pleased to tell you that this isnāt within our scope and weāre not required to worry about it as we will not design our JVMs or essentially have a need to.
(iii.) At runtime JVM will always suggest for a main method with the following prototype:
The above syntax is very strict, and if we make any changes to it, then, we will get a runtime error exception saying:
Further-more; it will be interesting to note that, even though the syntax is very strict, some changes are acceptable, which are:
ā¢ Instead of āpublic staticā, we can take static public and this is not only acceptable in the main method, but also applies anywhere else in Java, that is, the order of modifiers is not important.
ā¢ We can declare string array in any of these forms.
Ex;
ā¢ Instead of āargsā, we can take any valid Java identifier.
Ex;
ā¢ We can replace string array with āvar-argā parameter.
Ex;
Asides from these four possibilities, still some more changes may be possible.
They are:
ā¢ We can declare main method with the following modifiers:
- Final
- Synchronized
- strictfp
see below:
Youāll seldom get to see this type of main method above. As you can see āString[]ā is replaced with āVar-argā and āargsā is replaced with āYoutubeā.
Quiz:
Which of the following main method declarations are valid?
In conclusion, these are the various things to beware of in relation to syntax.
But as regards functionality, the following, we should beware of:
Case 1
Observe below:
This is known as method overloading, and as seen above, overloading of the main method is possible, but JVM will always call āString[] argsā main method only, the other overloaded method, we have to call explicitly, like a normal method call.
Case 2
Inheritance concept is applicable for main method, hence while executing a class, if a child class doesnāt contain main method, then the parent classās main method will be subsequently executed.
Case 3
This case is similar to the case above, a small adjustment is employed in the form of method hiding.
Lastly, for the āmain()ā, inheritance and overloading concepts are applicable, but overriding concept is not applicable. Instead of overriding, method hiding is applicable.
In conclusion to the main method session, letās discuss some of the stable enhancements that came in the early years, version 1.7 version to be specific, moving on.
From the version 1.7 upwards, a special significance of main method was established, as a lot of programmers were misusing the role of the main method.
Observe:
As seen above, until the 1.6 version, if the class doesnāt contain a main method, then we will get a runtime exception saying; āNoSuchMethodError: Mainā, but from 1.7 version onwards, instead of āNoSuchMethodError: Mainā, we will get a more elaborate error information.
Tabulated error message for the two versions, from the code above:
Consider another example:
The behaviour of the code in their respective versions below;
Hence, to run a Java program from the version 1.7 upwards, a main method is mandatory.
From 1.7v onwards, main method is mandatory to start program execution, hence, even though, class contains āstatic blockā it wonāt be executed, if the class doesnāt contain main method.
Finally, in conclusion to the main() section, letās show the algorithm of both versions, before and after the mandatory use of main()s. This helps to clear the difference in both versions as regards program execution.
For 1.6V
Oldest comments (0)