0

Sometimes in android, when creating custom adapters for listviews, programmers use a static class (usually named "Holder") that holds instances for custom sub-views. For example:

public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    final Holder holder;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.child_item, null);

        CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkBox_child);
        TextView tvName = (TextView) convertView.findViewById(R.id.textView_child_episodeName);

        holder = new Holder();
        holder.checkBox = checkBox;
        holder.tvName = tvName;

        convertView.setTag(holder);
    }
    else {
        holder = (Holder) convertView.getTag();
    }

    ...
    ...

    return convertView;
}

static class Holder {
    TextView tvName;
    CheckBox checkBox;
}

I understand this is a lot faster because each reference is saved and created only once.

In my code, instead of a static "Holder" class, I created a non-static class:

public class Holder {

    private TextView tvName;
    private CheckBox checkBox;

    public TextView getTextView() {
        return tvName;
    }

    public void setTextView(TextView tv) {
        this.tvName = tv;
    }

    public CheckBox getCheckBox() {
        return checkBox;
    }

    public void setProgressCheckBox (CheckBox cb) {
        this.checkBox = cb;
    }
}

And I use this non-static class. Now the question is - does it work the same? And if not, will setting the inner non-static class fields static make it basically the same in terms of speed?

UFC Insider
  • 838
  • 1
  • 7
  • 19

2 Answers2

0

Yes there is a difference. It is not android related, it's more of a OO-concept. Speed is a detail of the runtime implementation that you cannot necessarily foresee during development by simply choosing static vs. instance.

See Java Static vs Instance and Static vs Instance Variables: Difference?

Community
  • 1
  • 1
0

Always use static where possible. A non-static inner class has a reference to an instance of the outer class that owns it. A static one does not. That means that a non-static one takes more memory and can make it more difficult to clean up memory (because it adds extra references to the outer class). It also means you can't instantiate one without an instance of the outer class.

A static class does not have those restrictions. You can create one without an instance of the parent, and it doesn't keep references so won't ever cause a leak. Use them whenever possible.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • He's asking the different between a `Holder` class that's static in the parent class vs. a `Holder` class that's its own individual class. – DeeV Feb 25 '16 at 23:17
  • Will I get the same benefits if inside that public class I'll create an inner static class that's called "Holder" ? I mean, the static inner class will be located inside a public non-static class (not inside the adapter class) – UFC Insider Feb 25 '16 at 23:23
  • @DeeV Can you maybe address my question in the last comment? – UFC Insider Feb 25 '16 at 23:40
  • @UFCInsider There are no significant performance differences between the two approaches. @Gabe is right in that if you make `Holder` a non-static *inner* class (I.e., the holder is inside the Adapter), then there is some slight overhead. The only decision you have to make is if you want the pros-cons listed in the links in `bitrecycling`'s answer. – DeeV Feb 26 '16 at 03:08
  • The main reason you see people use static-inner classes in examples is A) It's cleaner and B) ViewHolders are often unique to the Adapter so there's rarely a reason to bring them outside (or make them public for that matter) – DeeV Feb 26 '16 at 03:09