Static, private and final variables

Asked

Viewed 513 times

0

I have a doubt that has been troubling me for a long time:

  • What is the advantage of creating a static and final variable (both private and public)?

  • If I have a primitive variable or simple classes (String for example), they have final values and are private. They should be static?

  • In the previous case, what is the impact of memory on an Android device?

As far as I know, creating variables of types that interact with the context of the application can happen a memory leak, so I know this is a "bad practice". However, what about simpler variables?

*Note: I installed a plugin in Android Studio that told me to put end even on local variables and that are argument. I did not understand the real need, and so I did not hesitate to ask here, for who knows.

1 answer

1


Final variables are only constant, meaning they cannot receive new values.

Static variables are class variables, that is, they do not depend on the creation of an object to be used and all objects in that class will have access to the value of this variable.

Private and public variable are only access restrictions that you configure. Private only object itself accesses and public anyone.

The combination of definitions above will depend on the strategy and logic you are implementing. I’ve never heard of a performance difference between them. At least in training, even the officers I’ve done never raised that question.

Class variables in place of primitives (Integer instead of int) came to add resources that primitives do not have (e.g., check if it is null). It has less performance, but it is so insignificant that it is not worth not using thinking about it.

The part of the context you quoted I didn’t understand.

Android Studio suggestions aim to increase code efficiency. For example: if you create a variable that does not change value and no other method accesses it, why not create it as constant and local within the method that uses it?

  • 1

    The reason the compiler requires a local variable to be final has to do with the fact that it’s used in a Inner class. The issue of Context has to do with the destruction/recreation of Activities. If there is a static reference to an Activity/Context when it is destroyed/recreated it will not be collected by the Garbage Collector, giving rise to a memory leak.

  • So that’s the thing about Context. However, as for the final variable, the plugin suggests me to use even the variables that are arguments in a constructor! I don’t use them in a nested class, only pass such variables to the private class...

  • 1

    This has to do not only with the last paragraph of the answer, but also with the "good practice" that values passed to the constructor should not be changed.

  • Yes, I get it. But I thought it was more about security or memory efficiency

  • 1

    @GH_ The memory for a variable Static (class variable) is only allocated once. A variable is not Static (instance variable) is allocated by each instance.

  • @ramaral, then the best practice, would be (if the variable is private and static only) nullify its values once Activity was destroyed? or has nothing to do with it? If the variable is final, static and private, would it be basically just accumulating memory (if there are more activities)? Confer?

  • 1

    A static variable, whether private or public, final or not, if initialized, its value (memory) is maintained throughout the life of the application. A non-static variable has its memory occupation released when it is no longer referenced and when the Garbage Collector understands. If you have static variables that reference Context(Activity is a Context) you must "undo them" before Activity is destroyed.

Show 2 more comments

Browser other questions tagged

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