tags:

views:

55

answers:

2

I am new in android world. I am trying to learn it. I have a question about inner classes in android code. Why and when do we need to creat inner class. We can create an individual class and by creating its object we can access its methods. So why do we need to create inner class and when it is necessary to create as inner class.

One advantage which i know is that we cant extend more than one class. So by creating inner classes we can extend more classes. What else ??

//Shan

+1  A: 

It's just for organization. There's normally not a need for inner classes. It just makes a lot of things easier because you can access the containing class' member variables

Falmarri
any other advantage rather than organization and making things easy ??
SilentCoder
What more is there?
Falmarri
This is wat i am asking.
SilentCoder
That's all code is, organization and readability.
Falmarri
+3  A: 

I think this really depends on what level of access you want to grant the potentially inner class to the outer class's fields. Also consider what level of access classes (or instances of) outside the outer class (and / or package) need to have to the inner class.

Specific to Android, I think it's more of a convention than a requirement. Sometimes I create inner classes merely for the convenience of avoiding the need to create a new file for 2 or 3 lines of functional code. This may not meet approval from absolutely everyone, but is easy to re-factor if issues come up. There is some good info on the topic at the developer site under Designing for Performance (scroll to the bottom).

Hope this is helpful. Best of luck!

Brandon