Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for inner/anonymous classes [closed]

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.

like image 820
David Avatar asked Sep 24 '12 07:09

David


People also ask

What are the disadvantages of using inner classes?

Inner classes have their disadvantages. From a maintenance point of view, inexperienced Java developers may find the inner class difficult to understand. The use of inner classes will also increase the total number of classes in your code.

Why would you use an anonymous class rather than an inner class?

Anonymous classes enable you to make your code more concise. They enable you to declare and instantiate a class at the same time. They are like local classes except that they do not have a name. Use them if you need to use a local class only once.

Do inner classes have to be private?

Inner Class You just need to write a class within a class. Unlike a class, an inner class can be private and once you declare an inner class private, it cannot be accessed from an object outside the class.

Which of the following is true about anonymous inner class?

18) Which of the following is true about the anonymous inner class? Explanation: Anonymous inner classes are the same as the local classes except that they don't have any name. The main use of it is to override methods of classes or interfaces.


2 Answers

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.

like image 91
Frank Pavageau Avatar answered Oct 04 '22 02:10

Frank Pavageau


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

like image 41
cl-r Avatar answered Oct 04 '22 00:10

cl-r