Hi All,

Can i Compile a java program without a single line in a main method.
If s, how it works. Please explain.
Help Me.

Use the javac command to compile a java program.
There are NO requirements for any methods in a class to be able to compile it.

Use the javac command to compile a java program.
There are NO requirements for any methods in a class to be able to compile it.

If there is no main method in a java class, will it run or not...?

A class without a suitable public main(...) method can only be called from another class. If you want to run a class from the java or javaw command it must have the method.

ok fine.... i do have a "public static void main(string[] args)" method, but it does not contain a single line, i know it will run sucessfully. But i wanna know the flow... how it works..?

If the main() method is there but no code inside, you should be able to run the class fine, but it will produce nothing. Usually, if I have main() method in a class object, I would implement quick tests for the class's functionality to see whether or not the class implementation works. I don't use it to be my real program.

public class DoNothing {
  int myInt;  // class variable

  // contructor
  public DoNothing(int val) {
    myInt = val;
  }

  // main method
  public static void main (String[] args) {
  }
}

// when compile using command line
// >javac DoNothing.java
//
// when run using command line
// >java DoNothing
//
// there is no result and the program should end gracefully.

Edited 6 Years Ago by Taywin: n/a

This article has been dead for over six months. Start a new discussion instead.