Java: static and final keywords
Static and final keywords in Java are at the core of the language and it is important to have an in-depth understanding of their function.
If you’ve every written a Java program of any scale, you’ve come across the “static” keyword. Your first program probably looked something like this:
There it is, right in the main method!
In short; fields with the static keyword belong to the class, they are class methods and variables.
When learning object oriented programming in a class or from a book, you’ve probably come across the analogy that classes are like blueprints. Let’s continue to build on that analogy; if we have a blueprint for a house (you can build many houses with that blueprint, just like you can make many objects/instances using the class), on that blueprint you may have information that describes the blueprint itself and not the house. Therefore if a we have a static variable in a class, that variable holds information about the class itself. I hope that analogy makes it easier to understand; if not, take a look at the example below:
Variables without the ‘static’ keyword are called instance variables so the userName variable is referred to as an instance variable of the User class.
Now, for our main method:
Notice the difference in how we set the instance variable ‘userName’ vs the static variable ‘description”.
When we set the instance variable ‘userName’, we do so by first creating and object/instance of the User class and then setting its ‘userName’ variable. In order to set the static variable ‘description’, we do so by setting it on the class itself via ‘User.setDescription(“My User Class”);’.
Therefore, by setting the ‘description’ variable to static, there is only one such field per class whereas there are many ‘userName’ fields (one per object that’s created).
How could this be useful?
Well, one practical purpose that a class variable could serve is keeping count of how many instances of that class we have. For example:
We’ve added a ‘count’ static variable to our User class and we increment it in our constructor. Now, each time we create an instance of the User class, we will have a variable keeping track of how many instances were created.
One last thing to note is that of static variables are not initialized with a value, they are “automatically” initialized with a default value which are:
short etc): 0
primitive floating points(
object references: null
Static methods are methods that belong to the class.
One important thing to note is that static methods do not have access to instance variables; they can only access other static fields.
If you attempt to access a instance variable via a static method, the compiler will let you know with an error message like:
java: non-static variable this cannot be referenced from a static context
This is because instance variables do not exist until an instance has been initialized; whereas static variables are created when a class is declared. Instance methods on the other hand, can access static variables.
Accessibility: Static fields or methods can be labeled private only if they are intended for in-class use only. If they are to be used outside the class then they must be labelled either protected or public
In a nutshell, final keyword is Java’s version of labelling a variable as a constant.
Final keyword prevents the variable from being re-assigned a different value, so once a value is assigned, it cannot be re-assigned to something else. When you declare a variable and label it final, it has to be initialized as well!
Code Convention: In Java, as with many other languages, constant variable names are always all caps.
A good existing exmaple is pi(represents the ratio of the circumference of a circle to its diameter).
The code above will result in the following error:
java: cannot assign a value to final variable PI
In the Math class, the PI variable is labelled with a final keyword; making it a constant and as we discussed, variables with a final keyword cannot be re-assigned!
Using the final keyword on methods prevents them from being overriden and using the final keyword at class level prevents that class from having subclasses (other classes cannot inherit from a class with a final keyword).
I hope you learned something today!