Here is an interesting use of reflection in Java. The following class is HelloWorld with no semicolons. The interesting thing about Java is that methods can return void which cannot be used in an if statement. By using reflection you can invoke a method that returns void, and have it return an Object.
| public class HelloWorldNoSemicolons { public static void main(String[] args) { try { if (null == System.out.getClass().getMethod("println", new Class[] { Class.forName("java.lang.String") }).invoke( System.out, new String[] { "Hello World!" })) { } } catch (Exception e) { } } } |
0 comments:
Post a Comment