I was asked this question in an interview.
How to print message on console without using main() method?
Yes, one of the way is static block but in previous version of JDK not in JDK 1.7.
class Withoutmain{
static{
System.out.println("Message: Your message can be print on console without main() method");
System.exit(0);
}
}
Output:Message: Your message can be print on console without main() method (if not JDK7)
Output:Error: Main method not found in class A3, please define the main method as: public static void main(String[] args)
Reference