[Solved] Java class without main method will run?


Q1. can we compile this java class? Note we don’t have a main method in this class.

Yes, that class should compile. There’s no requirement that says you need a main method in every class for it to compile. (Most of your classes won’t have their own main method.)

Q2. Is there any way we can print the message “Hello” which is there in the class?

Yes, you can load the class in another class that does have a main method. When your class loads the static initializer block will be executed, and the message will be printed.

solved Java class without main method will run?