Variables can be declared inside a conditional statement. However you try and access b
in a different scope.
When you declare b here:
if(a == 1) {
int b = 0;
}
It is only in scope until the end }
.
Therefore when you come to this line:
b = 1;
b
does not exist.
More Related Contents:
- Why can't we declare variables in if statement?
- Error: ‘else’ without ‘if’
- In Java, are enum types inside a class static?
- Why does a Try/Catch block create new variable scope?
- Spring Java Config: how do you create a prototype-scoped @Bean with runtime arguments?
- Declaring and initializing variables within Java switches
- What is the difference between a local variable, an instance field, an input parameter, and a class field?
- Java, “Variable name” cannot be resolved to a variable
- How do I create a static local variable in Java?
- What do curly braces in Java mean by themselves?
- Problems with local variable scope. How to solve it?
- Using variables outside of an if-statement
- Why is it not possible to shadow a local variable in a loop?
- Accessing a static variable via an object reference in Java
- Changing array in method changes array outside [duplicate]
- what is none scope bean and when to use it?
- I can pass a variable from a JSP scriptlet to JSTL but not from JSTL to a JSP scriptlet without an error
- What is the default scope of a method in Java?
- Java local variable visibility in anonymous inner classes – why is ‘final’ keyword required?
- What is the difference between @ApplicationScoped and @Singleton scopes in CDI?
- Should I use “this” keyword when I want to refer to instance variables within a method?
- Servlet redirect to same page with error message
- how to perform character concatenation in java [duplicate]
- How to write a Unit Test?
- Obtaining a powerset of a set in Java
- Is there a fixed sized queue which removes excessive elements?
- Why is this java Stream operated upon twice?
- Java 8 LocalDateTime is parsing invalid date
- Constantly Update UI in Java FX worker thread
- Transparent blurry view which blurs layout underneath