2 luke003 Luke0032016.01.31 02:40 questions

Java white tries to analyze the code. Ask God for help to look at is not correct; the code is as follows:

Public class Test{

Private static int i = 0;
Private static int j = 0;

Public static void main (String[] args) {


Int i = 2; //i is 2 instead of 0 because of the proximity principle
Int k = 3;
{
Int j = 3;
System.out.println ("i+j is\t" +i+ "\t" +j);
}

K = i+j;
System.out.println (k is +k);
System.out.println ("J is +j"); / / J here is 0 because the program block is executed first, then
/ / is a static variable is the last.
}

}

The 3 answer

Caozhy
Caozhy   Ds   Rxr 2016.01.31 06:23
Int i = 2; //i is 2 instead of 0 because of the proximity principle
This is what the nearest. This is the definition of variables, initial value. If this is not a I but to find another one, then the compiler should offer variable duplicate definition error.

System.out.println ("J is +j"); / / J here is 0 because the program block is executed first, then
This is not the nearest, but impossible to access your j code block
{
Int xxx;
}
XXX = 1; / / compiler errors

That is the time to visit the nearby principle variables, if two variables are accessible to (this is the premise, take the nearest scope).
Wojiushiwo945you
Wojiushiwo945you   Rxr 2016.01.31 05:57

This is in fact the local variables and class member variable scope, not what the principle of proximity problems. First of I local variables defined inside a main method scope is the whole method, so the main use is the local variable I; secondly, the local variable J is defined in the operating cycle, the scope is the body of the loop, and so the cycle body outside the j for the member variable J.

Fk002008
Fk002008   2016.02.01 00:01

The scope of the problem variables, not the nearest principle

Scope limit {} block
The scope of {}
The scope of the class {}

Other languages such as Python on a variable access is the principle of LGB, a global local - built

In Java is BMC block, method, class

The program method block, it is restrict the scope of a block, so block of things only in the internal block
If you are in the block internal print this j, it must be within the scope of block value
The block, the value of this variable will be invalid

In short note its current variable access in which you can use {} {}, to understand the principles of the nearest work

Csdn user default icon
Upload...
Upload photo
Insert a picture