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.
- use
public static void main(string args[]) { ... } - 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'. - 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:
- we not modify static
mainmethod, although need empty body class loaded. - we not use static initializer, use instance initializer instead.
- 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
Post a Comment