Aegidius
 Plüss   Aplulogo
     
 www.aplu.ch      Print Text 
© 2021, V10.4
 
  JavaOC
 
 


Do you show this ugly Hello World in your first Java lesson?

  class HelloWorld
{
  public static void main(String[] args)
  {
    System.out.print("Hello world!");
  }
}
 

How do you explain then what is public, static and the String array? Better show this full-fledged OO program and say: "When the HelloWorld object is created, the constructor is executed".

  class HelloWorld
{
  HelloWorld()
  {
    System.out.print("Hello world!");
  }
}
 

You compile the source program as usual with javac HelloWorld.java (or your favorite IDE). To run the program without main, you just replace java by javaoc (resp. javaw by javaocw). To have backward compatibilty, javaoc resp. javaocw will invoke main, when it is present.

 
 
 

 

There is no need of a main-method from a logical point of view. To run a program, just use a small operating system utility that creates an object instance.

 
How it works
JavaOC parses the command line and uses JNI (Java Native Interface) to create first a Java VM (Virtual Machine) and then an instance of the given Java class while passing the command line arguments to the constructor found by reflection.