Printing message on Console without using main() method

后端 未结 10 1792
情歌与酒
情歌与酒 2020-11-27 03:04

I was asked this question in an interview.

How to print message on console without using main() method?

10条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 03:19

    If you don't want to use static block too, it can be done following way

    public class NoMain {
    
        private static final int STATUS = getStatus();
    
        private static int getStatus() {
            System.out.println("Hello World!!");
            System.exit(0);
            return 0;
        }
    
    }
    

    However, please note that this is for Java 6 version. Its not working in Java 7 which is said to be supported in Java 8. I tried with JDK 1.8.0_77-b03, which is still not working

提交回复
热议问题