Thursday 28 December 2017

2.2 Trick in Java

1. Hello World without main method.

This was possible before 1.7. Logic behind this is , Static block executes before main method.
It will compile but it will not run in 1.7 and above.

to make it run you must have a main class
class Trick1 {
    static {
        System.out.println("Hello World");
        System.exit(0);
    }

    public static void main(String[] args) {}
}



2. Hello World without using semicolon.


No comments:

Post a Comment