Java Error - Illegal Modifier for Parameter - Only

2020-01-29 05:41发布

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?

标签: java
8条回答
家丑人穷心不美
2楼-- · 2020-01-29 05:52

Your have modified your question to ask:

I want to understand why its 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. The 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's 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.

查看更多
▲ chillily
3楼-- · 2020-01-29 05:55

you can not use public static modifier for a local variable. Do any of the followings

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

or declare it as a member variable

public static final String Name = "Robin Wilson";

public static void main(String[] args){

 }

Remember that the final is the only modifer of the local variables

查看更多
手持菜刀,她持情操
4楼-- · 2020-01-29 05:57

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.

查看更多
兄弟一词,经得起流年.
5楼-- · 2020-01-29 05:57

Your can not declare a local variable(variables inside methods are local variables) as public static. Instead, the following code will work:

public static void main(String[] args){
        final String Name = "Robin Wilson";
    }
查看更多
兄弟一词,经得起流年.
6楼-- · 2020-01-29 05:57

as you are Declaring the String variable as public static final String Name = "Robin Wilson";

According to java rules, This String name is Local variable as you are declaring it in Main method. So only final is permitted here. you can define it as ** final String name="Robin Wilson";** in main method. for Local variables only final is permitted.

查看更多
Rolldiameter
7楼-- · 2020-01-29 06:09

its mean you should write your static function outside the "main" block. It will work fine. Enjoy

查看更多
登录 后发表回答