Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Activity - Final class or not?

According to this SO question and performance benchmarks

  • Why defining class as final improves JVM performance?
  • http://www.javamex.com/tutorials/java_final_performance_vs_non_final_timings.shtml

We can assume that defining classes as public final class is better for performance.

Should we do the same with Android Activities, as they are mostly not used to subclassing ?

public final class LoginActivity extends Activity { ... }
like image 574
Marek Sebera Avatar asked Aug 04 '26 14:08

Marek Sebera


1 Answers

It's usually a safe bet to make all your classes either abstract or final.

This way, it's clear whether a class needs to be designed for inheritance.

If you later find out that for some reason you need to inherit from a concrete (non-abstract) class - which is usually a sign that there's a bit of asymmetry in your overall design - you can still remove the final keyword and make sure that your class is fit for inheritance.

So by doing this you eliminate the waste of having to design all your classes for inheritance, and at the same time you get maintainable code with clear intentions.

like image 120
yeoman Avatar answered Aug 06 '26 03:08

yeoman