CodeNewbie Community 🌱

Cover image for Main Method in Java

Posted on

Main Method in Java

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?image
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:image
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: image
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; image
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?

Here’s why; the JVM is also a software program; image

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:image

The above syntax is very strict, and if we make any changes to it, then, we will get a runtime error exception saying:image
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.
• Instead of “args”, we can take any valid Java identifier.
• We can replace string array with “var-arg” parameter.
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:image
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”.

Which of the following main method declarations are valid?image
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:
imageThis 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: image
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:image
The behaviour of the code in their respective versions below;image
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

While the enhancement that came in 1.7V:

Oldest comments (0)