Why in ViewHolder pattern should the ViewHolder class be static?

前端 未结 2 857
感动是毒
感动是毒 2021-02-07 00:46

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

2条回答
  •  囚心锁ツ
    2021-02-07 01:22

    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();
        }
    }
    

提交回复
热议问题