Java Error - Illegal Modifier for Parameter - Only final Permitted

What's wrong with the below Code

public static void main(String[] args){
        public static final String Name = "Robin Wilson";
    }

String Reference Name shows Compilation Error - Java Error - Illegal Modifier for Parameter Name - Only final Permitted

Am okay with the below given suggestions, but I want to understand why its not permitted, though both are static?


Solution 1:

Your have modified your question to ask:

I want to understand why it is not permitted, though both are static?

Variables inside a method exist only on the stack frame. The JVM creates a new stack frame every time a method is invoked, and it is discarded once the method completes.

The public keyword is used on classes, methods and fields to control access. There is no concept of access that could be applied to a stack (local) variable. It only exists inside the method when it's called, and can only be accessed from within the method.

The static keyword is used on fields to denote that only one such member exists across all instances of a class, and on methods to create them as members of the class that do not require an instance. There is no concept of a static state for anything on the stack; it is temporary. The stack frame and all the local variables on it cease to exist once you return from a method call.

Basically, neither make any sense when talking about a local variable.

Solution 2:

How to fix it

public and static cannot be used inside a method definition. So this error is telling you that the only modifier allowed for a variable defined inside of a method is final.

You fix it by either by removing the offending modifiers:

class MyClass
{
    public static void main(String[] args){
        final String Name = "Robin Wilson";
    }
}

or moving the variable definition out of the method like this

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

    public static final Name = "Robin Wilson";
}

Explanation

To understand why, you need to understand what each of the three modifiers (public and static and final) means on its own. String Name just says that we are keeping track of a String and calling it Name.

public

class MyClass
{
    public String Name = "Robin Wilson";
}

public says that any part of the program can read it (otherwise it could only be read by code written in the MyClass class).

public specifies what other code has access to it. Inside a method this doesn't make sense. Variables defined inside methods can only be accessed while inside that method, and once the method is complete they are thrown out. So it would be impossible for those variables to be public.


static

class MyClass
{
    static String Name = "Robin Wilson";
}

static says that the Name variable is a part of the class itself, not of an instance of the class. In other words, all instances of the MyClass class share the same Name variable.

static specifies how it is accessed (either on an instance of the class or through the class itself). Inside a method this doesn't make sense, because local variables are discarded after the method closes, so nothing else will be accessing it.


final

class MyClass
{
    final String Name = "Robin Wilson";
}

final says that the value of Name will never change once it has been assigned.

final describes how the variable is going to be used. It makes sense within a method, because it is not about access.

Solution 3:

You can't declare this inside main, put it outside the method, you want it as a [class member]:

public static final String Name = "Robin Wilson";
public static void main(String[] args) throws IOException {  }

Otherwise(I don't think this is what you want) just remove public static from there and simply write:

public static void main(String[] args){
    final String Name = "Robin Wilson";
}