Using static finals in Android activity

Why is the use of static final variables recommended to declare constants only on final variables? Using static sounds is logical when there are many instances of the class, but this argument is correct when used for Android activity. In fact, since the class instance will be around even after the operation is completed and, ultimately, garbage collection, it seems that all these constants will still be in memory until the class loader is started.

Also, does the built-in non-static compiler final variables (ints and String) contain exactly the same as for static final variables?

+3
source share
2 answers

They are static so you can read them from other classes. Static constants are mainly used to send broadcasts and the like. And there can be many examples of activity.

+4
source

Another thing is that you don’t even need to create a class object to access the constant. For example, if you want to get PI, you do not need to instantiate the Math class first, just use the class itself

Log.d("LogTag", String.valueOf(Math.PI));
+4
source

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


All Articles