We all have heard many times that main method is the entry point for any java program.
So whenever a program is executed, jvm starts searching for main method with
public static void main(String args[]){}
Signature.
And then whatever follows within main method starts getting executed.
But what if we omit the main method in a java program and still want the program to get executed.
There is still the way, if you are using version lesser than JDK 1.7
Solution is use static block
Try this example on your editor and execute it.
class noMainMethod{
static{
System.out.print("getting executed without main");
}
}
So the solution is write your code in static block as shown above.
Note: Remeber this won't work if you are on JDK 1.7 or above version.
All you will get as output will be something like this
Output:
Error:Main method not found in class noMainMethod, please define the main method as: public static void main(String[] args)
--------------------------------------------------*-----------------------------------------------------------
If you are looking for a reference book on java then we recommend you to go for → Java Head First
So whenever a program is executed, jvm starts searching for main method with
public static void main(String args[]){}
Signature.
And then whatever follows within main method starts getting executed.
But what if we omit the main method in a java program and still want the program to get executed.
There is still the way, if you are using version lesser than JDK 1.7
Solution is use static block
Try this example on your editor and execute it.
class noMainMethod{
static{
System.out.print("getting executed without main");
}
}
So the solution is write your code in static block as shown above.
Note: Remeber this won't work if you are on JDK 1.7 or above version.
All you will get as output will be something like this
Output:
Error:Main method not found in class noMainMethod, please define the main method as: public static void main(String[] args)
You may also like to read:
--------------------------------------------------*-----------------------------------------------------------
If you are looking for a reference book on java then we recommend you to go for → Java Head First
Click on the image link below to get it now.
To avoid the error, the SIB body should end with System.exit(0). This statement ensures that the compiler will not search for main() after execution of the SIB.
ReplyDeleteSIBs & IIBs and also constructors can be used to do so..
ReplyDeleteThank you Devina & Ashutosh for adding points to discussion on the given topic.
ReplyDeleteFor novice programmar, I would like to inform them here,
SIB is static initialization block
IIB is instance initialization block
I have already explained SIB in above post. Will be adding link to IIB on the post shortly.
Thank you readers <--> Java Radar
Why this static block doesn't work for JDK 1.7 or above?
ReplyDeleteWhy this static block execution without main method doesn't work in JDK 1.7 or above ?
ReplyDelete