access object in main class from a OnClickListener - is this a safe way?
<p>I'm new to android and I have not done any Java programming for probably near on 2 years.</p>

<p>I'm following the tutorials at the android developer resources.
More specifically <a href="http://developer.android.com/resources/tutorials/views/hello-formstuff.html" rel="nofollow">http://developer.android.com/resources/tutorials/views/hello-formstuff.html</a>
Using the EditText widget.</p>

<p>Now the code in the example runs fine (creating the listener in the onCreate method)</p>

<p>However, when playing around I like to define the listener elsewhere, simply because it separates the code out to make it easier to read and manage,</p>

<p>So here is my class with all other stuff taken out</p>

<pre><code>public class HelloFormStuf extends Activity {
private EditText edittext;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(btnOnClickListener);
    edittext = (EditText)findViewById(R.id.edittext);
    edittext.setOnKeyListener(etOnKeyListener);

}
private OnClickListener btnOnClickListener = new OnClickListener() {
    public void onClick(View v) {
        Toast.makeText(HelloFormStuf.this, "Beep Bop", Toast.LENGTH_SHORT).show();
    }
};

private OnKeyListener etOnKeyListener = new OnKeyListener() {

    public boolean onKey(View v, int keyCode, KeyEvent event) {
        // if the event is a key-down event on the enter button
        if ((event.getAction() == KeyEvent.ACTION_DOWN) &amp;&amp; 
                (keyCode == KeyEvent.KEYCODE_ENTER)) 
        {
            Toast.makeText(HelloFormStuf.this, edittext.getText() ,Toast.LENGTH_SHORT).show();
            return true;
        }
        return false;
    }
}; }
</code></pre>

<p>Now the original tutorial the edittext is final.</p>

<pre><code>final EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
    // If the event is a key-down event on the "enter" button
    if ((event.getAction() == KeyEvent.ACTION_DOWN) &amp;&amp;
        (keyCode == KeyEvent.KEYCODE_ENTER)) {
      // Perform action on key press
      Toast.makeText(HelloFormStuff.this, edittext.getText(), Toast.LENGTH_SHORT).show();
      return true;
    }
    return false;
}});
</code></pre>

<p><strong>My question is, is this safe to do it this way compared to the tutorial?  Because the big difference is now really is that the EditText object is now no longer final and is declared outside the class, but initialized inside the onCreate</strong></p>

<p><strong>Is this safe? such as there will be no little quirks laying around the corner to cause bugs.  Does making the EditText object not final really matter?</strong>  </p>

<p>My button is final, its listener is declared elsewhere and android is happy, I guess because I'm not altering the button or getting any properties of the button.  Where as if I keep EditText final, my listener code cannot resolve edittext (The EditText object).</p>

<p>Can I carry on doing this or is there another way (other then declaring it all in the onCreate method).</p>
