Execute main() multiple times without using any other function or condition or recursion in Java
Given task is to execute main() multiple times without using any other function and without recursion() and without error. Given condition is that if executing main() n times then you can only call him (n-1) times.
Solution:
class Test { // static block static { main(new String[] { "Hello" }); } public static void main(String[] args) { System.out.println("Hii"); } } |
Hii Hii
Explanation: Static block is executed even before the main() executed. Here first, main() get called by static block and then JVM(Java Virtual Machine) call the main(). So, main() is executed two times by calling only one time.
Recommended Posts:
- Java Multicasting (Typecasting multiple times) Puzzle
- Main thread in Java
- Is main method compulsory in Java?
- Valid variants of main() in Java
- GFact 48 | Overloading main() in Java
- Static Block and main() method in Java
- Replacing 'public' with 'private' in "main" in Java
- How to overload and override main method in Java
- What are the main differences between the Java platform and other platforms?
- Understanding public static void main(String[] args) in Java
- Does JVM create object of Main class (the class with main())?
- Understanding "static" in "public static void main" in Java
- Recursion in Java
- Java and Multiple Inheritance
- Returning Multiple values in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



