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条回答
Lonely孤独者°
2楼-- · 2020-01-29 06:13

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";
}
查看更多
Emotional °昔
3楼-- · 2020-01-29 06:15

Static declaration of a component makes in available on a class level. Declaring component in a method makes in available in method's stack memory hence can only be accessed through an object. Static belongs to the whole class. Therefore their is no point in declaring a variable static. You'll even get the compile time error if you try to do so. enter image description here Final keyword has nothing related to memory.

查看更多
登录 后发表回答