Why are there so many inner classes in Android?

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

I am confused by these inner classes.

+4
source share
5 answers

They are often the most effective way to implement design.

The inner class can access the private members of the class that contains it, so using the inner class allows you to share functionality between classes without the need to add access methods for private variables.

+7
source

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

Check out this article on inner classes: Inner classes: So what are inner classes anyway? .

+5
source

Simply put, when converted to bytecode, inner classes are "rebuilt" like outer classes in a single package. This means that any class in the package can access this inner class. The owner / prisoner / father classes of private fields turn into protected fields, as they are accessible to an external inner class.

OWASP Recommendation

Basically, it's just a “shortcut” that compromises the security of your own design.

Therefore, Android is not needed in this sense.

+3
source

I assume you already did C / C ++. These inner classes are not Android specific. They come from Java. In Java, the stacks (which we live in C / C ++) do not exist the same. Think of Java byte code as a blob of a binary executable that exists within a single function (sort of writing all your code inside a main function in C / C ++). But Java allows you to be “Object Oriented” and localize your code in classes for different tasks. It also allows you to infer from another class and create it at the same time. This is what you see in all the examples. The link provided by "Macarse" explains this for the Java programmer.

+2
source

It may also interest you:

Android: AsyncTask recommendations: private class or public class?

Not about why, but which prefers internal or external classes for AsyncTasks, one of the most prone classes to be used as internal.

0
source

Source: https://habr.com/ru/post/1315365/


All Articles