Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to print message on console without main and static block in java?

Tags:

java

In an interview I have asked this question:Without using static and main how could we print message on console?Is it possible?

like image 972
shree18 Avatar asked Aug 29 '13 08:08

shree18


Video Answer


1 Answers

You could define a custom class loader that prints your message :

public class MyClassLoader extends ClassLoader {
    public MyClassLoader(ClassLoader other) {
         super(other);
         System.out.println("Hi there");
         System.exit(0);
    }
}

Then run the java command :

java -Djava.system.class.loader=MyClassLoader

(don't need to add a class as parameter)

like image 102
Arnaud Avatar answered Oct 13 '22 10:10

Arnaud