2

The main() method is starting point of all java programs. All predefined methods are declared/defined in some class like println() method is defined in PrintStream class Now WHERE is the main() method declared/defined? Its not present in the Object class, where I presumed it to be . Or is it that the main() method is a user-defined method. If so, why can't we say

    public static void main(int args){}

or

    public void main(String args){}

Please someone explain this, Thanks in advance.

8
  • 2
    the main method is defined in the class you compile/run Commented Jun 23, 2017 at 6:33
  • 2
    this looks like a question posted minutes ago... maybe the homework for the weekend somewhere ..... Commented Jun 23, 2017 at 6:35
  • 1
    You can perfectly define those two methods. They just aren't valid entry points for a Java program, because Java expects a static method, named main, returning void, and taking an array of Strings as argument. Commented Jun 23, 2017 at 6:36
  • Did you read javadoc main method ? Commented Jun 23, 2017 at 6:37
  • yes your right ,JRE calling exactly same format as you said .so main() method is some were declared. were it was declared this is my actual dout Actually this question was asked in interview which i was attended.i given ans exactly as you said but he is not satisfied with ans @JBNizet Commented Jun 23, 2017 at 6:46

2 Answers 2

1

You can say:

public void main(String args){}

It's just that your program won't run, but it does compile.

The main method seems like magic at first sight, doesn't it?

It looks like a user-defined method (it is). yet all programs start here.

The trick here is java. When you run your program, you do a command like this, right?

java MyClass

Immediately after that, Java finds a .class file named MyClass and finds a class called MyClass in that file. Then it will automatically look for a main method with the exact signature:

public static void main(String[] args)

And then Java calls this. If you declare it any other way, Java can't find it.

Note that this is not a language feature. The JLS does not mention this at all. It's just how the java command is designed.

Sign up to request clarification or add additional context in comments.

2 Comments

you are right ,but My actual Questions is it was predefined or user defined method.
It's a user defined. Maybe I was not clear enough, but my first sentence is saying that you can declare it another way. The program still compiles but it won't run. I am kind of implying that it is defined by you. Sorry! @Srinu
1

Simply because the java program (i.e., java.exe on Windows) was written to use public static void main(String[]) as the entry point. This is not a fundamental feature of the language. It's entirely possible that some other program could use a different method as the entry point.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.