HOWTO · Java
修復 Java cannot be resolved to a variable 錯誤
修復 Java cannot be resolved to a variable 錯誤
本指南將教你如何修復 Java 中的 cannot be resolved to a variable 錯誤。
為此,你需要了解程式語言的範圍。繼續閱讀此緊湊指南以瞭解更多資訊並修復此錯誤。
修復 Java 中的 cannot be resolved to a variable 錯誤
在 Java 程式語言中,我們使用大括號 {} 來標識類、函式和不同方法的範圍。
例如,看看下面的程式碼:
public static void calculateSquareArea(int x) {
System.out.println(x * x);
}
在上面的程式碼示例中,變數 x 的範圍被限制在大括號 {} 內。你不能在此範圍之外呼叫或使用它。如果你嘗試,則會出現無法解析為變數錯誤。
這意味著它無法檢測其範圍內變數的初始化。類似地,如果你建立一個 private 變數,你不能在建構函式中呼叫它。
它的範圍超出了界限。這是不言自明的程式碼。
public class Main {
public static void main(String args[]) {
int var = 3;
// scope is limited within main Block;
// The Scope of var Amount Is Limited..........
// Accessible only Within this block............
}
public static void Calculate(int amount) {
// The Scope of Variable Amount Is Limited..........
// Accessible only Within this block............
}
}