I was asked this question in an interview.
How to print message on console without using main()
method?
If you want to print a welcome message before the main method is executed in Java, a static block is the best place to do that. What is a static block? A static block is nothing but a code block just like instance block. The static block does not depend on any object.
print(): print() method in Java is used to display a text on the console. This text is passed as the parameter to this method in the form of String. This method prints the text on the console and the cursor remains at the end of the text at the console.
The PrintStream class of Java provides two more methods to print data on the console (in addition to the println() method). The print() − This method accepts a single value of any of the primitive or reference data types as a parameter and prints the given value on the console.
public class Foo { static { System.out.println("Message"); System.exit(0); } }
The System.exit(0)
exits program before the jvm starts to look for main()
(Note: This works only with java 6. Even if it compiles with JDK 7's javac
it cannot be run with its java
, because it expects a main(String[])
method.)
public final class Main { static { System.out.println("Hello World"); System.exit(0); } }
The static block is first executed as soon as the class is loaded before the main();
method is invoked and therefore before main()
is called, System.exit(0)
initiates VM shut down.
The System.exit
method halts the execution of the current thread and all others dead in their tracks. When System.exit
is called, the virtual machine performs two cleanup tasks before shutting down.
First, it executes all shutdown hooks that have been registered withRuntime.addShutdownHook
. This is useful to release resources external to the VM. Use shutdown hooks for behavior that must occur before the VM exits.
The second cleanup task performed by the VM when System.exit
is called concerns finalizers. If either System.runFinalizersOnExit
or its evil twin Runtime.runFinalizersOnExit
has been called, the VM runs the finalizers on all objects that have not yet been finalized. These methods were deprecated a long time ago and with good reason. Never call System.runFinalizersOnExit
or Runtime.runFinalizersOnExit
for any reason: They are among the most dangerous methods in the Java libraries. Calling these methods can result in finalizers being run on live objects while other threads are concurrently manipulating them, resulting in erratic behavior or deadlock.
In summary, System.exit
stops all program threads immediately; it does not cause finally blocks to execute, but it does run shutdown hooks before halting the VM. Use shutdown hooks to terminate external resources when the VM shuts down. It is possible to halt the VM without executing shutdown hooks by calling System.halt
, but this method is rarely used.
In a file called A.java
class Con {
String hi = "\n\nHello World\n\n";
}
You just have to compile the program on Windows. Not run it. :-P
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With