Skip to content
JackSparrow414
Go back

Notes on Java’s static Keyword

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:

Explanation of member access and invocation rules for Java static methods


Share this post:

Previous Post
Oracle Query Notes (Part 2): INSTR, LPAD, and RPAD
Next Post
JavaScript Error Notes (Part 1)

Comments

Questions, corrections, and experiences are welcome. Sign in with GitHub to comment; both language versions share this discussion.

Comments are available on the live site only.