Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing message on Console without using main() method

Tags:

java

I was asked this question in an interview.

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

like image 720
Nandkumar Tekale Avatar asked Dec 22 '11 14:12

Nandkumar Tekale


People also ask

How can I print welcome message before 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.

What is the method used to print a data on the console?

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.

Can we print anything without using Println function?

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.


4 Answers

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.)

like image 172
Bala R Avatar answered Oct 07 '22 08:10

Bala R


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.

like image 20
Lion Avatar answered Oct 07 '22 09:10

Lion


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

like image 32
Peter Lawrey Avatar answered Oct 07 '22 09:10

Peter Lawrey


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 25
Arnaud Avatar answered Oct 07 '22 08:10

Arnaud