java中使用switch case報錯case expressions must be constant expressions

1、發現問題express

private int CLICK_QUERY = 1;
private int CLICK_RESET = 2;
ide

@Override
public void onClick(View v)
{
 int tag = (Integer) v.getTag();
 switch (tag)
 {
  case CLICK_QUERY:
   query();
  break;
  case CLICK_RESET:
   reset();
  break;
 }
}
編譯時一直報錯:CLICK_QUERY 和CLICK_RESET——case expressions must be constant expressions
spa

 


2、解決問題get

case後面必須跟常量,必需要常量,將上面兩個變量聲明爲final便可。it

private final int CLICK_QUERY = 1;
private final int CLICK_RESET = 2;
io

相關文章
相關標籤/搜索