Santhoshkannan
Santhoshkannan

Reputation: 55

why the main method in java should be small letter(main) and not as caps letter(Main)?

is there is any specific reason in java, the main method should be a small letter

yes "Main" and "main"

This compiles

public class ManiMethod {
    public static void main(String[] args) {

    }
}

why this doesn't compile

public class ManiMethod {
    public static void Main(String[] args) {
    }
}

Upvotes: 0

Views: 4520

Answers (7)

Dinesh
Dinesh

Reputation: 1086

Main and main will be two different methods if both are present in any class. Whenever you try to run any class say TestClass using java TestClass, then JRE will search for the method with name main(String[] arg). Else it will throw exception.

Upvotes: 1

GMc
GMc

Reputation: 1774

Because those are the rules.

The rules also include that it must be static, public and accept a single parameter of type String[] (I.e. an array of String).

Since java is case sensitive Main is completely different to main.

There is also a convention (some would say standard) that classes start with a capital letter and methods start with a lower case method. So to extend your question to the extreme, you might as well be asking why isn't it called "abcde" instead of main?

As a result of all the above (and no doubt more) the developers of the java runtime environment elected to call the method that is the entry point for a java program main.

Upvotes: -1

Sachin Muthumala
Sachin Muthumala

Reputation: 765

Java is case sensitive. "Main" is not the main method. If you compile and run using Main it is not working.

 Error: Main method not found in class q1, please define the main method as:
     public static void main(String[] args)
 or a JavaFX application class must extend javafx.application.Application

Upvotes: 1

CryptoFool
CryptoFool

Reputation: 23079

You are incorrect. This code:

public class ManiMethod {
    public static void Main(String[] args) {
    }
}

will compile fine. It won't run as a Java program though.

In general, uppercase vs lowercase naming conventions, are just that, conventions. There's nothing stopping you from giving methods names that start with uppercase letters. Your second code block will compile just fine.

Then there's the question of if java can run this class. It can not, because the definition of a class that Java can run is one that has a static method named "main", which is different than a method named "Main".

If you combine your two class definitions, so you have two methods, one named "Main" and one named "main", it will both compile and run. The method named "main" will be the method that gets executed.

Upvotes: 0

Mureinik
Mureinik

Reputation: 311163

The second snippet most definitely compiles - it just doesn't run because it doesn't have a main method.

Why main and not Main? Because that's what the language's designers decided. Section 12.1.4 of the JLS specifies how exactly the main method's signature should be defined.

Upvotes: 3

Mark Melgo
Mark Melgo

Reputation: 1478

The main is the method called when a Java application begins. Keep in mind that Java is case-sensitive. Thus, Main is different from main.

You can find the more detailed explanation here

Upvotes: 1

Thomas
Thomas

Reputation: 488

1st of all Java is case-sensitive, "Main" and "main" are different for Java. Secondly, Java was inspired by C/C++ which also uses the main() function as a starting point for a program.

Java will accept any function names even Main() but will specifically look for static main() method as the starting point of program.

Upvotes: 0

Related Questions