0

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.

4
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by Taywin
0

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.

0

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

0

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.

0

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

0

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 by Taywin: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.