tags:

views:

197

answers:

3

I am a new fish in Android development. While reading books and Android source code I found out that there so many inner classes in the Android application. Why does Android need so many inner classes?

I am confused by these inner classes.

+5  A: 

They are often the most efficient way of implementing a design.

An inner class can access the private members of the class which contains it, so using an inner class allows a split of functionality between classes without the need to add accessor methods for private variables.

Al Sutton
Still, it's better to have those members package instead of private to avoid extra accessor methods generated under the hood.
alex
+3  A: 

Inner classes are not just in Android. I guess you need to understand why they are good in some cases.

Check this article about Inner classes: Inner classes: So what are inner classes good for anyway?.

Macarse
+1  A: 

I am guessing you have been doing C/C++ before. These inner classes are not Android specific. They come from Java. In Java, stacks (which in C/C++ we live by) do not exist in the same manner. Think of Java byte code as a blob of binary executable that exist inside one function (kind of like writing all of your code inside the main function in C/C++). But Java allows you to be "Object Oriented" and localize your code in classes for diffrent tasks. It also allows you to derive from another class and instantiate it at the same time. That is what you see in all the examples. The link that "Macarse" has provided explains this for a Java programmer.

Sam