0

Adding a final keyword in the main method parameter works fine. Why does it not give any compiler error / exception since I have modified the standard main method of Java?

 public class StackOverFlow {

    public static void main(final String... args) {
        System.out.println("Hi");

    }
}

Now see if I code:

public class StackOverFlow {

    public static void main (String... args) {
        
        String[] str = {"I ", "haven't ", "received ", "my ", "answer." };
        args[0] = "hi";
        System.out.println(args[0]);
        args =str;
        for(int i=0; i<args.length; i++) {
            System.out.print(args[i]);
        }

    }

}

For the above coding when you run the program by passing an argument as:

javac StackOverFlow Nisrin

My program output is

hi

I haven’t received my answer.

Now the same thing with the final keyword

public class StackOverFlow {

    public static void main (final String... args) {
        
        String[] str = {"I ", "haven't ", "received ", "my ", "answer." };
        args[0] = "hi";
        System.out.println(args[0]);
        args =str;
        for(int i=0; i<args.length; i++) {
            System.out.print(args[i]);
        }

    }

}

It gives me an error: the final parameter, args, may not be assigned.

Because now I am assigning str to args.

This means I have done a big difference by adding the final keyword in the parameter and making it constant in the main method, the very entry-point of a Java Program. I am changing the signature of the main method. Then why am I not getting any compile error or runtime error?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nisrin Dhoondia
  • 145
  • 1
  • 10
  • @Andreas Ok I do understand that args is an Object String array so it is final/constant but it does not make every single elements in it constant. But my question is of I am changing the signature of main method. – Nisrin Dhoondia Oct 28 '18 at 05:01
  • The fact that you can still run the program after adding `final` shows that you did not change the *signature*. The signature is determined by the *method name*, and the *parameter types*. Parameter modifiers (`final` and annotations) don't matter. – Andreas Oct 28 '18 at 15:37

2 Answers2

6

final marks the reference as constant. It doesn't actually change the type. Also note changing the signature of main is never a compiler error, but it can frequently cause a runtime error.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Can you please explain what you mean by "it can frequently cause a runtime error." – Nisrin Dhoondia Oct 28 '18 at 04:29
  • 1
    I mean, if you change the signature of `main` to no longer be an entry-point the code will still compile. You just won't be able to run it. Thus a runtime error (not a compiler error). – Elliott Frisch Oct 28 '18 at 04:30
  • Ok, so you mean that it doesn't remain a main method any more which the JRE can use it to run the program. Please do make a point to check my modified question and kindly guide. – Nisrin Dhoondia Oct 28 '18 at 04:36
0

final only tells that "the reference is a constant. Don't try to change it". That means so far the main method can use it properly.

The args parameter is usually used for getting information from the execute command. You may never have any reason to change its value in your program any more.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
TranNgocKhoa
  • 423
  • 1
  • 6
  • 20