Advantages of Inner Class

Asked

Viewed 334 times

5

Since I started programming for Android (I believe due to Google examples), I have a habit of creating internal classes for functionalities related to Activity.

Example: If the Activity connect to the bank (and if the request is specific to this screen), create an internal class that extends from AsyncTask.

I would like to know if there is any advantage in using this approach?

Is there any gain in performance?

Or is it just a structural matter?

4 answers

4


With regard to the Android and specifically the use of Inner Class in a Activity his concern should be focused on the problems that he(use) can bring with regard to Memory Leaks.

The life cycle of a Activity is not fully controlled by us and can be destroyed at any time by the OS.

One Inner Class, that is not declared Static, or a Anonymous Inner class maintain an implicit reference to Activity which may prevent the memory allocated by it from being claimed, when destroyed and recreated, during a configuration change (device rotation for example).

Hence any advantage(it is debatable whether there is any) than the use of Inner Class can bring is conditioned to the existence or not of the danger of Memory Leaks.

For reading:
How to Leak a Context: Handlers & Inner Classes
Activitys, Threads, & Memory Leaks
When Exactly is it Leak safe to use (Anonymous) Inner classes?

4

The use of Inner Class makes total sense in several cases. If no other class needs/uses it, make it private. If it does not require exclusive access from other members of other classes, make it static because it will make it allocate less memory. In official documentation:

Use a non-static nested class (or Inner class) if you require access to an enclosing instance’s non-public Fields and methods. Use the Static nested class if you don’t require this access.

However, another point of view is if your class is already gigantic. I mean, with an excessive number of lines. Several developers don’t like to use Inner Class not to increase the number of lines in your class.

Like everything else, use wisdom and common sense to enjoy a healthy architecture.

3

Note: this is a complementary response to @ramaral.

The notion of being careful with memory leakage is real and very important. However, after reading the two cited articles I would say that, at the very least, they have the wrong.

What to say is that the problem is not in the internal classes but in you reference activities or other disposable objects in objects that persist.

It makes as much sense to blame internal classes as it does threads.

Now, whether it is an internal class or not, static or not, the temptation to reference Activity is great and the chances are that the developer will eventually pass the reference to the other class in some way. In other words: any object with a reference to Activity can cause the Leak memory. It can even be an Inner class within a method in another class that accesses the received Activity in the parameter of that method.

In some examples in the cited articles, it is said that the virtual machine does not clean threads. Correct, but this is true in any case. So even the "good" example of the article still features a memory Leak. It may not leak Activity, but it certainly leaks threads, as they are never destroyed. This can be as bad as more memory consumption, as the app will have an increasing number of threads doing the same thing at the same time.

Therefore, rather than worrying about the subtleties of the internal classes, the developer should try to clean up the allocated resources or at least check if they are no longer allocated.

As for the use of internal classes, as the others have said, the biggest question is structural, since they are compiled separately from the main classes in files .class individual.

However, it is interesting that internal classes can access private methods of the main class is vice versa. This is an advantage if you want to keep encapsulation of some elements.

Anyway, if you’re giving visibility to some method or attribute only so a particular class can access them, you might want to consider using an internal class or another design option.

In Java in general, the use of internal classes is extensive, from the code of Java itself to the encouragement of the use of Lambdas in version 8. Who adheres to functional programming techniques can not be afraid and I have seen many renowned software making extensive use of internal classes.

Summary: Do not be afraid of internal classes. Even the best developers can’t understand or predict all the subtleties that can cause problems in an application or application. But it’s imperative, as the author of the article did, that you learn techniques to monitor and diagnose problems and not just trust that everything will work out because you followed the X or Y rules.

  • If the Inner class is not declared Static she will always have a reference to Outer class. If it is an activity or other that can be destroyed by Android, You can provoke memory Leaks. That is to say "... whether it is an inner class or not, static or not" should the Outer class be an Activity.

3

It’s just structural! In the compilation process, each Inner Class is done separately and has even a little more attention than the Garbagecollector(I cannot guarantee that it will perform better). In practice, they are much faster to be developed and leaves everything more organized.

If you look at any official source code of Oracle , will notice a lot the use of Inner Class.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.