Search code examples
onclicktextviewfinal

Declare TextView as final


Sorry, noob android dev here, I am trying to understand why eclipse wants my TextView to be declared as final.

    TheStack theStack = new TheStack(10);
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView display = (TextView) findViewById(R.id.tv1);
    Button b0 = (Button) findViewById(R.id.b0);

    b0.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            theStack.push("0");
            display.setText(theStack.getStack());
        }
    });

}

Solution

  • Short answer: Because you use display in inner class.

    Long answer: Why are only final variables accessible in anonymous class?