[英]Non-static variable this cannot be referenced from a static context JFrame
[英]How to edit instance variable from inside a method? non-static variable sum cannot be referenced from a static context
public class Sum{
int sum = 0;
public static void sum_do(String array[]){
int tmpi = 0;
for (int i=0; i<array.length; ++i){
tmpi = Integer.parseInt(array[i]);
sum += tmpi;
}
}
}
我想修改 sum 變量,但我無法這樣做。
./Sum.java:9: error: non-static variable sum cannot be referenced from a static context
sum += tmpi;
^
1 error
我對 Java 真的很陌生,所以這可能是一些非常簡單的東西
您嘗試在靜態上下文中訪問非靜態變量,請嘗試:
static int sum = 0;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.