Local Variables in Construct versus in Method Body

In summary, the conversation discusses the issue of declaring local variables in Java, specifically the error encountered when trying to declare a variable named "final" inside an if statement. The conversation also touches on the concept of reserved keywords in Java and the importance of avoiding them as variable names.
  • #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);
}
 
Physics news on Phys.org
  • #2
You can declare a variable inside the { ... } block after an if statement, but the variable only exists inside that block, not for the rest of the function.

But "final" is a reserved keyword in Java. You can't declare a variable called "final" anywhere in a Java program, just like you can't declare a variable called "if" or "int".

Here's a complete list of the reserved words. If you are just starting to learn Java, don't worry about what they all mean - just don't try to use them as variable names! http://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html
 
Last edited:
  • Like
Likes 1 person
  • #3
yes that solved it
thanks
 

Related to Local Variables in Construct versus in Method Body

1. What is the difference between local variables in Construct versus in Method Body?

Local variables in Construct are declared within the class and can be accessed by all methods within that class, while local variables in Method Body are declared within a specific method and can only be accessed within that method.

2. Can local variables in Construct and in Method Body have the same name?

Yes, local variables in Construct and in Method Body can have the same name as they are declared in different scopes. However, it is considered good practice to use different names to avoid confusion.

3. How are local variables in Construct and in Method Body stored in memory?

Local variables in Construct are stored in the heap memory, while local variables in Method Body are stored in the stack memory. This means that local variables in Construct have a longer lifespan and can be accessed by multiple methods, while local variables in Method Body have a shorter lifespan and can only be accessed within the method in which they are declared.

4. Can local variables in Construct and in Method Body have different data types?

Yes, local variables in Construct and in Method Body can have different data types as they are declared separately. However, the data type of a local variable in Method Body must be compatible with the data type of the corresponding variable in Construct if they are used together.

5. How do local variables in Construct and in Method Body affect the performance of a program?

Local variables in Construct generally have a greater impact on performance as they are stored in the heap memory and can take up more space. Local variables in Method Body, on the other hand, have a smaller impact on performance as they are stored in the stack memory and have a shorter lifespan. However, the effect on performance is minimal and should not be a major concern when deciding where to declare local variables.

Similar threads

  • Programming and Computer Science
Replies
3
Views
793
  • Programming and Computer Science
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
2
Replies
47
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top