https://www.codecademy.com/article/variable-scope-in-java
Why is block level scope not included with method level scope?
They seem rather similar to me, if it's in regards to header and body for both
#3 Types of Variable Scope in Java
11 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @novel gulch! Please use
/closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Kinda true, as far as the language bothers to define, they're both just local variables
It's just it's not super obvious that a local variable declared inside nested { and } is not visible from outside these { and }
And variables declared as part of a loop's ( and ) conditionals is visible only in these conditionals and inside the loop
so anything written in () or {} cannot "leave" to a higher parent scope so to speak
the only exception, being return?
return doesn't expose the variable. Only its value.
Also beware with the (), that's only the for() constructs that can declare variables that way. Lambdas too, in a way, but they work rather differently.
myMethod(int myInt){ System.out.println(myint); }
does this count as "declaring the variable", myInt?
or this is merely declaring a parameter, which is different from declaring a variable?