how to print "hello world" without MAIN method and also without using STATIC and FINAL key word in java -


class {  public static void main(string args[]) {   } } 

i want print "hello world" without touch or use above code , without use of static , final keyword......

what different ways initiate code execution.

  1. use public static void main(string args[]) { ... }
  2. use static class initializer static { ... }. problem: need class load first. solution: attempt start main method, means need have @ least empty main. - op states cannot use 'static'.
  3. use instance initializer { ... }. problem: how instantiate instance. solution: initialize static field class instance. - op states cannot use static fields.

so left. assume can use empty main posted op. there on build 4th solution:

public enum play {   play;   { system.out.println("hello world!"); }   public static void main(string args[]) {  } } 

this should fulfill conditions:

  1. we not modify static main method, although need empty body class loaded.
  2. we not use static initializer, use instance initializer instead.
  3. to instantiate, , instance initializer started, use enum. enum's start initializer each instance, meaning each of enum constants.

also note output when try execute class without main:

$ java -classpath . play error: main method not found in class play, please define main method as:    public static void main(string[] args) or javafx application class must extend javafx.application.application 

so leaves clue potentially can use javafx.application.application. did not investigate trail further.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -