Java initialization variable increment
•
Java
Is it risky to initialize a global variable in increments of another global variable?
Example:
int a=0; int b=a++; int c=a++; int d=a++;
This should output:
0,1,2,3
Can the compiler read global values before another?
Solution
It will run as expected If you try to use a field before defining it, the compiler throws an error:
public class Foo { int a = b++; //compiler error here int b = 0; }
This is covered in JLS 8.3
In your case, if the output of the variable is not modified, it is:
a = 3 b = 0 c = 1 d = 2
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码