Java was a beautiful programming language for me to learn. I still love the way it forced me to write the neat and organized code, though I didn’t like the idea of writing extra lines just to write a “Hello world” and also the performance of the language. I think, it became popular because of Android adoption and will fade in upcoming years because of legal disputes and strategical changes of Google to replace it with some other language like Kotlin or Dart.
Among a lot of beautiful things from Java, one common goodie is setter-getters. I assume, you are familiar with the idea of setter-getters by now.
In case you are not- it is just a function allows us to set some value to a variable, which can be later retrieved using getter function.
Getter-setter is not the only way to data passing, but I believe its most commonly used among them. Obviously getter-setters don’t serve to all problems and others approaches have their significance in the case.
Anyway, in Java, a normal Class could have Constructor, Getter, Setter and Destructor. Every method/function is self-explanatory in general-
Constructor, to initializes required vars. Setter sets values to vars of Class. And Getters are used to retrieve the values of vars of the Class. And finally Destructor to clean up the vars, you don’t want to keep the vars in memory after you are done using it.
But, as a developer we might need it, if you use get-set. I believe it’s must to destroy all set vars if your framework is not taking care of it.
We, in company, use AngularJS in frontend and NodeJS in backend. While angular has destroy method and it takes care of scope vars but there is no such things in Nodejs inbuilt. More, if you set something globally, you need to remove it after you finish with it.
Approach is simple…
Set when you start controller.
Get as per need of the code.
Destroy when controller ends its task.
Or your code will end up storing a lot of unnecessary data in memory and will be slowing down the system.
And it can lead to worse things if your code is memory critical.