Search code examples
javaargsmain-method

How come there is no error if we name args[] as arhs[](or any other name) in java main method?


I'm a beginner in java.When executing a simple program I noticed that in main method

public static void main(String args[])

args[] can be given any name and it executes successfully. Why is that?


Solution

  • When you call a method, you don't care about what the names of its parameters are, do you?

    Say I have this method:

    public static void doStuff(int number) {}
    

    And I can call it like this:

    doStuff(10);
    

    Do I need to use the parameter name number anywhere in the calling code? No.

    Even if you call the method by reflection, (which I think is what actually happens when you run a java program) you don't need the parameter names.

    Method m = clazz.getMethod("main", String[].class);
    m.invoke(null, null);