title: Cannot refer to a non-final variable inside an inner class defined in a different method
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/1299837/cannot-refer-to-a-non-final-variable-inside-an-inner-class-defined-in-a-differen">Cannot refer to a non-final variable inside an inner class defined in a different method</a>  </p>
  </blockquote>
  
  
  
  <p>I have some problem in my program.</p>
  
  <pre><code>public class MainActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activityenter code here`_main);
  
      ImageView ob4 = (ImageView) findViewById(R.id.imageView4);
      ShapeDrawable d4 = new ShapeDrawable(new OvalShape());
  
  
      d4.setIntrinsicHeight(150);
      d4.setIntrinsicWidth(40);
      d4.getPaint().setColor(Color.WHITE);
      d4.setVisible(true, true);
      ob4.setImageDrawable(d4);
  
      Button btn_start = (Button) this.findViewById(R.id.button1);
      btn_start.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
          //Cannot refer to a non-final variable d4 inside an inner class defined in a      different method
          //But i don't want to make this field "final", becouse i will change color of this shape in future. Thanks.
          d4.getPaint().setColor(Color.RED);
          }
      });
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.activity_main, menu);
      return true;
  }
  }
  </code></pre>