- #1
friendbobbiny
- 49
- 2
I've posted a method below.
I'm experimenting with local variable declarations in java. Actually, a compiler error in eclipse has made me reconsider what I understand about local variables. Why can't the local variable, String final, be created inside my if statement? Why, instead, would Eclipse recognize final if it were declared before the if statement.
In the previous two questions, I questioned the rationale behind a rule. Is my understanding of the rule even correct? My memory suggests otherwise -- declaring local variables in a construct should be possible.
If it makes any difference, although I doubt it, I created the method under a class that inherits another class.
public String getShortDate(){
String need = "0";
String temporary;
if( getMonth() < 10){
temporary = String.valueOf(getMonth());
String final = need + temporary;
int month = Integer.parseInt(final);
}
I'm experimenting with local variable declarations in java. Actually, a compiler error in eclipse has made me reconsider what I understand about local variables. Why can't the local variable, String final, be created inside my if statement? Why, instead, would Eclipse recognize final if it were declared before the if statement.
In the previous two questions, I questioned the rationale behind a rule. Is my understanding of the rule even correct? My memory suggests otherwise -- declaring local variables in a construct should be possible.
If it makes any difference, although I doubt it, I created the method under a class that inherits another class.
public String getShortDate(){
String need = "0";
String temporary;
if( getMonth() < 10){
temporary = String.valueOf(getMonth());
String final = need + temporary;
int month = Integer.parseInt(final);
}