23

I am just trying to have a better understanding of the following pattern I regularly use to optimize ListView

My readings only pointed me to the fact that a static inner class is treated as top level class. What is the benefit of such a thing compared to a member class (non static)?

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Comment comment = getItem(position);
    ViewHolder holder;
    if (convertView == null){
        holder = new ViewHolder();
        convertView = LayoutInflater.from(context).inflate(R.layout.mylayout, null);
        holder.nickname = (TextView) ((ViewGroup) convertView).findViewById(R.id.nickname);
        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }

    holder.nickname.setText(comment.getMember_nickname());
    CharSequence
    return convertView;
}

public static class ViewHolder{
    TextView nickname;
}
5
  • 5
    because if it is not, the class is linked to the instance, which is totally unnecesary.
    – njzk2
    Nov 22, 2012 at 17:01
  • @njzk2: what would be the downside? Apr 24, 2013 at 15:51
  • @Jan1337z the downside is that you have a instance of the class object for each instance of your adapter if the ViewHolder is not static.
    – njzk2
    Apr 25, 2013 at 8:52
  • @njzk2: thats wasted memory - thats all? Apr 25, 2013 at 9:59
  • 1
    @Jan1337z wasted memory and wasted setup time to create the class instance and to destroy it. In this particular situation i think that's all, there are other cases of inner class where there are other drawbacks.
    – njzk2
    Apr 25, 2013 at 11:40

2 Answers 2

8

My opinion is that it is better to have the ViewHolder class static as it won't leak the Adapter.

If the adapter retains some heavy Collections or even Views (depends on each particular case), it would be great to keep control of which objects retain the Adapter.

Having a lot of objects instances of an inner class will have those objects referencing the Adapter, thus retaining it. You should be careful about how the tags are managed (if the views are cleaned/removed automatically there is no problem>).

4

One benefit of using static inner class, is that the inner class can be accessed from static methods, without having an instance of the outer class.

If the inner class non-static:

class MyOuter {
    private int x = 7;
    public void makeInner() {
        MyInner in = new MyInner();
        in.seeOuter();
    }
    class MyInner {
        public void seeOuter() {
            System.out.println("Outer x is " + x);
        }
    }
}

public static void main(String[] args) {
    MyOuter mo = new MyOuter();
    MyOuter.MyInner inner = mo.new MyInner();
    inner.seeOuter();
}

If the inner class is static:

class BigOuter {
    static class Nest {void go() { System.out.println("hi"); } }
}

class Broom {
    static class B2 {void goB2() { System.out.println("hi 2"); } }
    public static void main(String[] args) {
        BigOuter.Nest n = new BigOuter.Nest();
        n.go();
        B2 b2 = new B2();
        b2.goB2();
    }
}
3
  • 2
    I'm not sure if this completely addresses the question of the ViewHolder pattern... Mar 3, 2014 at 20:54
  • @j0k NathanZ wrote: "My readings only pointed me to the fact that a static inner class is treated as top level class. What is the benefit of such a thing compared to a member class (non static)?" My answer does not necesarrily refer to ViewHolder pattern, I just pointed out one of the benefits of using static inner class over non-static. As far as I can tell, the question was more about the benefits of using static inner classes than about the ViewHolder pattern. Mar 5, 2014 at 6:27
  • @IllyesIstvan I guess you wanted to reply to Igor, not me :)
    – j0k
    Mar 5, 2014 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.