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.

Recommended Answers

All 6 Replies

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...?

An application requires a main() method to run, an applet does 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.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.