当从For循环外部访问时是变量,如果在main方法&;中进行声明,则在Java中是不可能的;循环内初始化? class-Myclass{ 公共静态void main(字符串[]args){ int x;//在main方法中声明 如果(真){ 对于(int i=0;i

当从For循环外部访问时是变量,如果在main方法&;中进行声明,则在Java中是不可能的;循环内初始化? class-Myclass{ 公共静态void main(字符串[]args){ int x;//在main方法中声明 如果(真){ 对于(int i=0;i,java,for-loop,compiler-errors,initialization,declaration,Java,For Loop,Compiler Errors,Initialization,Declaration,这会产生一个错误:变量x可能尚未初始化 系统输出println(x); ^ 1错误 但是,下面的代码工作正常 class Myclass { public static void main(String[] args) { int x; // Declared in main method if (true) { for (int i = 0; i < 5; i++) { x = 5;//

这会产生一个错误:变量x可能尚未初始化 系统输出println(x); ^ 1错误

但是,下面的代码工作正常

class Myclass {
    public static void main(String[] args) {

        int x; // Declared in main method

        if (true) {
            for (int i = 0; i < 5; i++) {
                x = 5;// initialized inside loop
            }
        }

        System.out.println(x);// accessing outside for loop
    }
}
class-Myclass{
公共静态void main(字符串[]args){
int x;//在main方法中声明
如果(真){
x=5;//在if块中初始化
对于(int i=0;i<5;i++){
//x=5;
}
}
System.out.println(x);//访问外部if循环
}
}

在这两种代码中,唯一的区别是在第一种情况下,变量在“for loop”中初始化,而在第二种情况下,变量在“if block”中初始化。那为什么会有不同呢。请向我解释一下,因为我找不到真正的原因。

它是可以访问的,但程序可能永远不会访问for块。因为编译器不满足for循环之外的任何其他var初始化,所以它会给您一个错误。要编译它,必须使用默认值初始化变量:

class Myclass {
    public static void main(String[] args) {

        int x; // Declared in main method

        if (true) {
            x = 5;// initialized in if block
            for (int i = 0; i < 5; i++) {
                // x=5;
            }
        }

        System.out.println(x);// accessing outside if loop
    }
}
class-Myclass{
公共静态void main(字符串[]args){
int x=0;//在main方法和init中用默认值声明。
如果(真){
对于本代码中的(int i=0;i)

公共静态void main(字符串[]args) {


问题是编译器不知道当您访问它时,
x
将被初始化。这是因为编译器不检查循环体是否实际执行(可能很少有情况下,即使如此简单的循环也可能不运行)

如果条件不总是真的,也就是说,如果使用如下布尔变量,则if块也是如此:

int x = 0; // or use another default variable

作为人,您可能会说“但是
cond
被初始化为
true
,并且永远不会更改”,但编译器并不确定(例如,由于可能的线程问题)如果您将
cond
作为最终变量,那么编译器将知道
cond
在初始化后不允许更改,因此编译器可以内联代码以有效地拥有
If(true)
再次。

如果将If块中的条件从
true
更改为
false
,则会出现与
变量“x”可能未初始化相同的错误。
If(true)
编译器可以理解if块中的代码将始终运行,因此变量x将始终初始化

int x;

boolean cond = true;
if( cond ) {
  x = 5;
}

//The compiler will complain here as well, as it is not guaranteed that "x = 5" will run
System.out.println(x);
但是,当您在for循环内初始化变量时,可能会发生for循环从未运行,而变量未初始化的情况

int x;

boolean cond = true;
if( cond ) {
  x = 5;
}

//The compiler will complain here as well, as it is not guaranteed that "x = 5" will run
System.out.println(x);
publicstaticvoidmain(字符串[]args){
int x;//在main方法中声明
if(false)
{
x=5;//编译错误

for(int i=0;i在java
for
循环中,在运行时进行计算,因此编译器忽略了对循环内部变量初始化的检查,这就是“x”必须用值初始化的原因。

一般来说,在将变量初始化为某个任意值时要非常小心,因为编译器会抱怨-这实际上可能是正确的(显然,在这种简单的情况下不是这样)@JiriTousek让代码无法编译并不能真正改善情况试图理解编译器抱怨的原因并发现设计中可能存在的缺陷可能会改善情况。像OP这样保证初始化的情况在我看来是罕见的。要么你有很好的理由对其进行初始化,要么你有一个合理的默认值初始化为,或者应该以一种不依赖于初始化的方式将代码重写为一个没有意义的数字。跳过这个思考过程,仅仅使用一个随机数对我来说似乎很危险。@JiriTousek我从未建议使用“随机数”。我从未遇到过任何没有(至少)随机数的用例一个默认值,如果仅适用于“未找到”场景。
int x = 0; // or use another default variable
int x;

boolean cond = true;
if( cond ) {
  x = 5;
}

//The compiler will complain here as well, as it is not guaranteed that "x = 5" will run
System.out.println(x);
 public static void main(String[] args) {

      int x; // Declared in main method
         if(false)
            {
                x=5; //compile error
                for(int i=0;i<5;i++)
                {
                    //x=5 initialized inside loop
                }

            }
            System.out.println(x);
        }