【问题标题】:What's wrong with the following code in java [duplicate]java中的以下代码有什么问题[重复]
【发布时间】:2016-01-31 09:03:48
【问题描述】:

我是 java 异常处理的新手。 我只是想弄清楚我的概念,但我遇到了以下问题。

这里是代码。

class Inn {
    public static void main(String... args) {
        try{
            String i="Asd";
        } catch(Exception e) {
            System.out.println(e);
        }
        //i=7;
        System.out.println(i);
    }
}

这是即将发生的错误。

G:\javap>javac Inn.java
Inn.java:13: error: cannot find symbol
                System.out.println(i);
                                   ^
  symbol:   variable i
  location: class Inn
1 error

【问题讨论】:

  • 为什么 jvm 找不到符号 'i' ??
  • 我重新格式化了您的代码,以便您可以更清楚地看到块范围。

标签: java compiler-errors cannot-find-symbol


【解决方案1】:
try { // Block A
    String i="Asd"; 
} // End of Block A
catch(Exception e) {
    System.out.println(e);
}

System.out.println(i); // 'i' does not exist in this scope

变量i 在代码block A 中声明,这意味着它只能从其内部访问(注意限制其范围的{ 花括号})。一旦执行流经过块A,变量i将不再存在于范围内。

话虽如此,如果你想让这个工作,你必须声明内部范围的字符串i out

String i = ""; // necessary initialization, without it you'll get a
               // "variable may have not been initialized" error
try { 
    i = "Asd"; 
}
catch(Exception e) {
    System.out.println(e);
}

【讨论】:

    【解决方案2】:

    i 变量在不同的范围内定义,并且在您尝试打印它时不可见。

    改写为:

    import java.io.*;
    class Inn
    {
        public static void main(String s[])
        {
            String i = null;
            try{
                i="Asd";
            }catch(Exception e)
            {
                System.out.println(e);
            }
            //i=7;
            System.out.println(i);
        }
    }
    

    这样,变量iprintln 语句在同一范围内。

    【讨论】:

      【解决方案3】:

      对于大多数编程语言,通常将 范围 与变量相关联。范围在外行术语中的含义是 - 可以使用变量或变量可见的代码部分。

      对于 Java(或许多 OOP 语言),我们通常有不同级别的 范围

      1. 类级别范围:类中定义的变量,即成员变量可以在类中的任何位置访问(为了简单起见,我将 static 修饰符保留在图片之外)。这些变量通常定义在类的顶部(约定​​)。请记住,这些变量需要在类的方法之外才能在类级别范围内。

        public class Example {
            private String a; // variable a can be accessed anywhere inside the class
        }
        
      2. 方法级别范围:方法中定义的变量只能在方法内部访问。当方法返回时,它们的生命周期结束。

         private int giveMeFive(){
            int a = 5; // Scope of a is within the method
            return a; // When a is returned then there the variable a dies :(
        }
        
      3. 循环级别范围:您在循环中定义的变量仅限于该循环和任何内部循环。不能在定义它们的循环之外访问它们。

         public static void main(String []args){
            for(int i=0;i<10;i++){
                System.out.println(i); // Only i is accessible here but not j
                for(int j=1;j<5;j++){
                    System.out.println(i+j); // Both i and j are accessible here
                }
            }
         }
        
      4. 块级范围:通常,大括号 { } 内的任何内容都定义了特定范围。在 Java 中,您通常可以访问一个变量,只要它是在同一组括号内或这些括号内的任何括号内定义的。

      在您的情况下,您在 try 块中定义了变量 i,因此一旦 try 块完成,该变量就不再 可见,因此您的 println 语句稍后无法找到在代码中打开。

           public static void main(String[] args){
               String i; // i defined outside the try block so it can be accessed after the try finished running
               try{
                   i= "Asd"
               }catch(Exception e){
                   Sytem.out.println(e);
               }
               System.out.println(i);
           }
      

      一切顺利:)

      【讨论】:

        【解决方案4】:

        这是因为 i 的范围恰好在 try 块中。

        【讨论】:

          【解决方案5】:

          变量 i 是在 try 块内定义的,它限制了 i 在 try 块内的范围。您需要在 try 块之外定义它。

          【讨论】:

            【解决方案6】:

            Java 是一种作用域语言,因此变量的可见性取决于它的定义位置。

            在计算机编程中,名称绑定的范围(名称与实体(例如变量)的关联)是计算机程序中绑定有效的部分:名称可用于指代实体。 Java 是词法范围的。

            Problem with "scopes" of variables in try catch blocks in Java

            因为您的string i 已在try 块中声明,所以println 方法无法找到它。 (超出范围

            【讨论】:

              猜你喜欢
              • 2018-12-21
              • 2012-04-11
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多