Java Error - Illegal Modifier for Parameter - Only final Permitted

前端 未结 8 717
既然无缘
既然无缘 2020-12-03 01:32

What\'s wrong with the below Code

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

相关标签:
8条回答
  • 2020-12-03 02:13

    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. Final keyword has nothing related to memory.

    0 讨论(0)
  • 2020-12-03 02:17

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

    0 讨论(0)
  • 2020-12-03 02:19

    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.

    0 讨论(0)
  • 2020-12-03 02:20

    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.

    0 讨论(0)
  • 2020-12-03 02:30

    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";
    }
    
    0 讨论(0)
  • 2020-12-03 02:31

    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";
        }
    
    0 讨论(0)
提交回复
热议问题