If you are a Java user, you have probably wondered whether or not you absolutely HAVE to use main method to create a program.
Honestly, the truth is that no you do not. It is not do or die.
You can easily run a Java program without a main method simply by making use of a static block.
This is a collection of statements which will get executed in Java, but only once you have loaded the class into the memory through Java ClassLoader.
This can also be known as a static initialization block, which will go immediately into the stack memory.
However, this is not the only way, there is much to be said about this and plenty of ways that experts have found around this.
Main method is not the only method for creating a program via Java.
Advertising links are marked with *. We receive a small commission on sales, nothing changes for you.
Is It Compulsory To Use Main Method In Java?
Now, we have to ask ourselves is main method is actually compulsory in Java.
We don’t mean like ‘are you breaking the rules of Java if you do not use main method’, we aren’t in school.
However, is it compulsory for things to run properly? Well, the answer is actually dependent on what version of Java you use.
If you use anything before JDK 7 then you will find that main method was not actually compulsory.
You would be able to write full code via a static block and it would run just fine. It would be executed the minute the class loads before the main().
This method would be used and before the main() is used. As the main is typically noted to be a static method anyway.
Once you give the command, the compiler will presume that Test is a class in which main() is present and since the load the blocks will be ready to be used.
So, in this it will run via a static block initially, then it will notice there is no main() and it will give an exception.
This being said, versions from JKD 7 and beyond this you do need to use main method.
This is because the compiler will first want to verify that the main() is actually there, and if it is not, you will get an error code come up.
However, let’s look a little more into this, because we are certainly not done yet.
Applets And Servlets
When you first learn about Java, you will learn that you need the main method, however, if you choose to specialize in the area you will be asked if Java can run without it, and the answer is yes, which for most learners is a shock.
You see, there are actually many types of methods such as Applets which will run if you do not have a main method, they will use something known as a life-cycle method in its stead, which will control execution.
Applet is also a program by Javaso, there is no issue there. Then there is Servlet which will come in a form similar to that of the well known Jetty.
It works on a callback mechanism, and it is a program by Java as well, so it will run via this as well.
Of course, there is also MIDlet, which will run on mobiles. It also uses a life-cycle method to function.
However, if you were asked if Java can run without main method, while the answer is yes, if you were asked if Java can run without a main method using Core Java, then the answer would be no.
Using Java Without Main Method
Most programmers who use Java will say that you can indeed run Java without main method simply by using a static block, and this is true… to a point.
A static block does allow you to execute the code before the main method is called.
However, you will be unable to initialize a class using a Java command.
You also won’t be able to use anything else without a “public static void main” method attached.
Should you try to do this, you can expect an error code to come up.
While you can use Java without a main method, although only from a static block, what you rally get is a basic program with a bit of code on a static block, probably a statement for print, a starting thread and some variable initialization.
Once you have removed main method it does work just fine, but you will get an error code come up every time you attempt to initialize the program through a command line.
Your code will look a bit different too, let’s say, naked, when you run without main method.
Don’t get us wrong it works just fine, but it’s a little bit like rowing a boat when you could just put an engine on the back.
So, while you can run a Java program without using main method through a static block or in a managed environment, you cannot do the same with core Java programs, as you simply will not be able to execute it at all.
To Conclude: Java Without Main Method, Yes Or No?
To put it plainly, technically sure, you could create a program and use a program with Java without using main method, although, you cannot do this all the time, and it probably should not be your go to.
Sure you can use static blocks and programs like Applet or Servlet, it is not recommended that this be a continuing style of coding for you.
You must also remember that you cannot initialize a core Java program when you do not have “public static void main” methods.
You simply will not be able to, JVM just won’t let you.
If it was us, we would simply stick to main method, but now you know, you could run Java programs without it.
Advertising links are marked with *. We receive a small commission on sales, nothing changes for you.