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?
Your have modified your question to ask:
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.
you can not use public static modifier for a local variable. Do any of the followings
or declare it as a member variable
Remember that the final is the only modifer of the local variables
How to fix it
public
andstatic
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 isfinal
.You fix it by either by removing the offending modifiers:
or moving the variable definition out of the method like this
Explanation
To understand why, you need to understand what each of the three modifiers (
public
andstatic
andfinal
) means on its own.String Name
just says that we are keeping track of aString
and calling itName
.public
public
says that any part of the program can read it (otherwise it could only be read by code written in theMyClass
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 bepublic
.static
static
says that theName
variable is a part of the class itself, not of an instance of the class. In other words, all instances of theMyClass
class share the sameName
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
final
says that the value ofName
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.Your can not declare a local variable(variables inside methods are local variables) as
public static
. Instead, the following code will work: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.
its mean you should write your static function outside the "main" block. It will work fine. Enjoy