Java Non-Access Modifiers: static, abstract, final
Continuing from the previous post, its time to talk about the second group of Java Modifiers — Non-Access Modifiers.
These modifiers are used to describe a specific behavior of a variable or a method.
I’ll focus only on the most common Non-Access Modifiers:
static
abstract
final
static Modifier
This modifier is used to create a class variable or a method which can be accessed without a class instance — object.
It’s like binding a variable or a method to the class name.
static variable
If we have a simple class like:
We access the creditCard
variable like: Person.creditCardNumber;
or in the standard way by creating an object:
Although we can have many instances of Person
class, we have only one creditCardNumber
variable, which is shared among all the instances.
This code:
Will result in "13"
being printed to the console. Thats because both first
and second
objects share the same creditCardNumber
variable. They both can access and modify it.
Many people mix the static
modifier with the Access Modifiers. They think that those two somehow affect each other. But the reality is, they don’t.
static
is only saying that we don’t need a class instance to access a variable or a method. It says nothing about where can we access those variables and methods from.
Key point is to use static
variable when we want to share the same variable between all the class instances. For an example, when we need to use constants which all the class instances need to have access to.
static method
Same as with the static
variables, we don’t need a class instance to call static
methods.
Here, method shoutSomething
can be called: Logger.shoutSomething();
static
methods can only access its parameters and other static
methods and static
variables. static
method cannot access variables and methods of the class that are not declared static
.
It’s a good idea to declare a method static
if doesn’t manipulate the class it’s in. Or if the method only operates on its parameters.
Good examples of classes with static
methods are util and helper classes. Like Math
or LocalDateTime
.
static block
Its a block of code used to initialize static
variables of a class.
Here we can see that we initialized something
variable to "smthing!!”
from the static
block.
static
block is really useful when we need several steps to initialize a static
variable. For example, if we want our static
attribute to be a List
of several elements within. We can do this from thestatic
block:
abstract Modifier
abstract Modifier can be used with a method or a class.
abstract method
When we declare a method as abstract
we do not provide its implementation in the class its located in.
For example:
Here we have doWork()
method which is declared as abstract
.
There is no implementation of the doWork()
method provided.
abstract class
A class which contains an abstract
method must be declared as an abstract
class.
We can see in the example that Airplane
class was declared abstract
.
Aside from abstract
methods, anabstract
class can contain non-abstract variables and methods, same as every other class.
An abstract
class cannot be instantiated.
So this is quite an odd combination. To recap, an abstract
class has variables and methods, same as every other class. But additionally, it has method declarations ( abstract
methods). And it cannot be instantiated. So how can this be useful?
The trick is, that when a class extends an abstract
class, it must provide an implementation for all of the abstract
methods of the parent class.
We enforce all subclasses of the abstract
class to contain a specific method with a specific name, return type and parameters. But leave it to subclasses to define what those methods do:
Note that both Bomber
and Transporter
planes have name
attribute set to "Ilyushin"
. They also both have doWork()
method but with different implementations.
This behavior is the basis for polymorphism, design patterns…
final Modifier
final
modifier can be used with classes, methods or variables.
final Class
When we declare a class as final
we cannot extend it with other classes.
So this code would be invalid:
final Method
When we declare a method as final
we cannot override it from the subclass.
Trying to override doTheDishes()
method from the subclass would result in an error:
final Variable
final
variables can be declared and initialized at the same time:
Or they can be declared blank and initialized in a static block:
Once initialized, their value cannot be changed. Trying to change the value of the TRUTH
variable results in an error.
If a final
variable is a reference to an object:
it cannot be reasigned, this code will result in an error:
but the Object it points to can be changed, code below is valid:
Conclusion
This concludes my two-part series on Java Modifiers 😅 there are a few more modifiers that I didn’t mention in this post. But I’d have to go into Multi-Threading to explain those.
Hope you find the post helpful, feel free to leave a comment or a suggestion. Show your support by clapping or a follow 😄
Stay tuned for more… Cheers everybody 🍸