Why it says that “Cannot refer to a non-final variable i inside an inner class defined in a different method”? [duplicate]
问题 This question already has an answer here: Cannot refer to a non-final variable inside an inner class defined in a different method 20 answers I have button click listener and in onCreate() method I have a local variable like onCreate() { super.onCreate(); int i = 10; Button button = (Button)findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { i++; } }); Why java asks for to make me final ? 回答1: When the onCreate() method