Printing message on Console without using main() method

后端 未结 10 1726
情歌与酒
情歌与酒 2020-11-27 03:04

I was asked this question in an interview.

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

相关标签:
10条回答
  • 2020-11-27 03:22

    Yes, one of the way is static block but in previous version of JDK not in JDK 1.7.

    class Withoutmain{  
     static{  
      System.out.println("Message: Your message can be print on console without main() method");  
      System.exit(0);  
     }  
    }  
    

    Output:Message: Your message can be print on console without main() method (if not JDK7)

    Output:Error: Main method not found in class A3, please define the main method as: public static void main(String[] args)

    Reference

    0 讨论(0)
  • 2020-11-27 03:23

    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

    0 讨论(0)
  • 2020-11-27 03:24

    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)

    0 讨论(0)
  • 2020-11-27 03:26
    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.

    0 讨论(0)
提交回复
热议问题