Hello everyone, I want to define a public variable in the main function. The system reports an error saying "illegal modifier for parameter chatroom, only final is permitted". If public is removed, the compilation is successful. However, even if public is removed, My variable is not final, because I changed it later. Why does the system say it must be final?
Public is used to modify the method or field in the class definition. Another occasion is to modify the class.
Cannot be placed in method.
Only final is permitted
The system means that if you need to add modifiers before the variables in the main function, you can only add final. In this case, your variable is final.
But if you do not add anything before the variable, it is a common variable, not final
Not only in the main method, defining variables in any method cannot declare public,
Public is declared as a domain, and the variables defined inside the method can only be used inside the method (for parameter transfer). The declaration scope is meaningless here, so the declaration is prohibited.
Only final is permitted
Only final is allowed.
This means that the variable can only be declared in the method with final modification as needed. In addition, other modifiers, including static, cannot be used, and the same error will be reported.