6

What's the best practise(design and performance wise) for anonymous classes and static inner classes?

Personally I would like to think that static inner classes provide better encapsulation and should give better performance as they dont have access to finalized variables outside their class. But I've never really questioned this.

I found one post about this but I felt it didn't actually answer the question about, just peoples personal thought about it.

Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
David
  • 375
  • 4
  • 11
  • Haven't thought about performance in that context. But design: Design follows function. I guess there are scenarios when static inners are "best practice" and others where anonymouses are. – Fildor Sep 24 '12 at 07:56
  • Performance will probably be negligible... But I'm kinda curious about it. – David Sep 24 '12 at 07:59

2 Answers2

4

Inner classes (static or not) have exactly the same access to their enclosing class' fields and methods as anonymous classes, the difference between static inner classes (actually called nested classes) and (regular, non-static) inner classes being that the static one need an explicit reference to an instance of the enclosing class to access something. Of course, when you need to do that, it's usually on the instance of the enclosing class that created the inner class, so it's easier and clearer to use a non-static inner class.

Examples:

  • Inner class (non-static)

    class A {
        private int field;
    
        private class B {
            public void doSomething() {
                field++; // Valid
            }
        }
    }
    
  • Nested class (i.e. "static inner class")

    class A {
        private int field;
    
        private static class B {
            public void doSomething(A a) {
                a.field++; // Valid
            }
        }
    }
    
  • Anonymous class

    class A {
        private int field;
    
        public void doSomething() {
            new Runnable() {
                @Override
                public void run() {
                    field++; // Valid
                }
            }
        }
    }
    

Whether you use that accessibility is another question. If you do access private fields of the enclosing class, there'll be an accessor generated, so it could impact the performance as the cost of calling a method is not the same as accessing a field, but it will probably be negligible in most cases. You should always write correct code first (both in terms of design and functionality) before doing micro-optimizations not based on any measurement. The JIT compiler does a lot of things for you anyway.

Frank Pavageau
  • 11,477
  • 1
  • 43
  • 53
  • 1
    +1 : ... write correct code first... – Jayan Sep 24 '12 at 08:18
  • There is no static inner class as they don't have access to parent class members (i.e. non static members will not be accessible from static context). we call those are static nested class, works like top level or independent class. – Leninkumar Koppoju May 17 '16 at 20:11
  • @LeninkumarKoppoju Indeed, they're called nested classes, I learned the correct name later. I'll update the answer. However, a nested class does have access to any member of the enclosing class _if it has a reference to an instance of that class_: `outer.privateInt = 42;` works, which is impossible from another top-level class. – Frank Pavageau May 18 '16 at 08:07
1

Have look in Java source code Collections and Pattern to have sample (they are in the src.zip in the JDK directory. In Eclipse, you can read the code with inline help

Read a book Effective Java, look for inner class to understand how works static, inner and other useful interface, enum and other classes

Jayan
  • 18,003
  • 15
  • 89
  • 143
cl-r
  • 1,264
  • 1
  • 12
  • 26