Braces is the code block
- With a name code blocks --if-else block, for loop code block, main code block method
- Also called code blocks, eg for body, the method main body
- Code blocks can be nested
Variable scope
- Code block where you can create and use variables
- Block can be used in the outer code block variable
- However, not using the inner code blocks in the variables in the outer code block. (Can use variables, also called a code block variable visibility. That is, the outer block of code creates a variable visible to the inner code blocks, a variable inner code blocks created in the outer layer does not block visible.)
- Namespace inner variable (or a scope) can not be repeated define variables outer code block, the outer code block may be used in
- No matter how many layers of nested code blocks, follow the above variables visibility rules.
Scopes and namespaces
- With a namespace variable can not be duplicate names
- In order to avoid conflict of variable names, so there must be a namespace
public class codeblock { public static void main (String [] args) { // inner code block in the scope (namespace) can be used to block the external variables. // In turn, external variables can not be used to block scope. // scope of the code block with the end of execution of the code block ends. int Outer = 100 ; { int inner = 10; System.out.println ( "outer value is" + outer + "inner value is." + Inner); } // block can be nested. The same life cycle rule scope. int A1 =. 1 ; { int a2 = a1 + 1; { an int 3a , = A2 + 1 ; System.out.println("a3=" + a3); } { an int 3a , = A2 + 10 ; System.out.println("a3=" + a3); } System.out.println("a2=" + a2); } } }