1

In Java when you need to access to an outer class member or variable from an inner class, you have to declare it final. See this question.

My question is: is this a good practice?

Particularly when I write code for android I often use solutions similar to this one:

final EditText textView  = (EditText) setUrlDialog.findViewById(
    R.id.dialog_text_set);
textView.setText(urlTw.getText());
alertDialogBulder.setPositiveButton(R.string.ok,
    new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            textView.getText()
            //Do something with the text
        }
});

Can this kind of solution cause performance problems? Are there any other reasons to avoid frequent use of this solution?

Community
  • 1
  • 1
Panciz
  • 2,183
  • 2
  • 30
  • 54

5 Answers5

2

Since the textView in the example is not supposed to be used all over the place, it gives you higher readability to do what you're already doing, since the variable is used close to its definition. Having to use the final modifier is just a technicality and causes no performance penalties whatsoever. More information.

Compare this with declaring textView as a field instead. If textView is a field, it is certainly not clear from the declaration in which context it is being used. It can also create a (minor) performance penalty as the inner class will need a synthesized accessor object to access the outer field if it is declared as private. See here for a discussion on accessor objects.

David Burström
  • 1,592
  • 14
  • 14
  • No performance problems, more local variable definition and a standard idiom = win. – Voo Dec 07 '11 at 18:30
1

I think declaration of variables at globally, then you don't have to use final modifier for it, And you can also access this variable through out the class and its inner class, also there is no any performance issue on that..

In your case...

If you declare EditText textView; at globally above onCreate() then yo don't need to use final modifier,

something like,

public class Test extends Activity {
    EditText textView;
    public void onCreate(Bundle savedInstanceState)
    {
     textView  = (EditText) setUrlDialog.findViewById(R.id.dialog_text_set);
textView.setText(urlTw.getText());
alertDialogBulder.setPositiveButton(R.string.ok,
        new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                textView.getText()
                //Do something with the text

            }
});
}
}

(If I am wrong then let me know for this..)

user370305
  • 108,599
  • 23
  • 164
  • 151
  • Yeah and you get hundreds of unnecessary instance variables in the outer class which makes it much harder to read. Performance advantages? Non existent, actually if you don't store the variable in the inner class it may get more expensive to access it (additional indirection, probable cache miss,..) or you store it twice which just increases the size of an instance (you get less into the cache theoretically) – Voo Dec 07 '11 at 18:27
0

Don't worry too much about "good practice" in that case because using "inner classes" is considered a bad practice itself.

Here you have more information on that issue:

Why so many inner classes in Android?

Community
  • 1
  • 1
erDave
  • 31
  • 4
0

Keep doing that. It is a cumbersome syntax already, but will improve with closures in java. Otherwise you would need to make a new class every time and that certainly has not a better performance.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

you declare EditText as private EditText textView ; then you can use the outer class varibles in your inner class .

user493244
  • 909
  • 7
  • 19