Table of contents
Open Table of contents
Article body
The static keyword means there is a single copy in memory. While reading code today, I noticed that every method used to query approval documents was static. That made me curious: what do we gain by declaring all those query methods static?
A static method can be called directly through ClassName.methodName, and it continues to occupy memory, saving the resources needed to create an object with new. If a method is called frequently, making it static is something to consider.
A static class is allocated memory when it is loaded and continues to occupy that memory, whereas a non-static class is allocated memory when it is created. Utility classes are generally declared static and called frequently.
During execution, Java loads the static parts first and then executes methods. If the class has a superclass, the superclass constructor runs first.
A static method can be used directly within a class without creating an object.
But in this approval-document query code, these methods aren’t being used as utilities. The static query methods are all in the same class, and they can be called directly without another object. Was this choice made because pending documents are queried frequently? Does calling static methods make that faster? But they keep occupying memory, don’t they? Personally, I think there are some issues with this implementation.
Additional note:
