DEV Community 👩‍💻👨‍💻


Posted on • Updated on

Java static and final, what are they and how to use them

When I first started coding in Java I struggled to understand the differences between static and final, what were they or when to use them, so I decided to write a short summary for the newcomers.

To the point! Let's see how we can use them in our code.

The final keyword

It can be used in variables, methods, and classes.

Final variables

Final variables are intended to serve as constants, values that shouldn't (and won't) ever change:

    class Car {
        public final int numberOfWheels = 4; 
Enter fullscreen mode Exit fullscreen mode

Even if we wanted to modify this value, the compiler would throw an error:

Car myCar = new Car();
myCar.numberOfWheels = 1;

>>> The final field Car.numberOfWheels cannot be assigned

Enter fullscreen mode Exit fullscreen mode

Final methods

Final methods cannot be overridden by any subclasses. Assume the class Car and the class Sedan:

class Car {

    public final int getNumberOfWheels() {
        return 4;

class Sedan extends Car {

    // This won't work because the method getWeight is final!
    public double getNumberOfWheels() {
        return 3;

Enter fullscreen mode Exit fullscreen mode

This can be useful in cases that, as the one described, the result or the behavior of the method should not change when called from subclasses.

Final classes

Using the final keyword in a class prevents it from being extended:

final class Pear {
    private double weight;
    private String color;

// This won't work because the Pear class is final! 
class MagicalPear extends Pear {

Enter fullscreen mode Exit fullscreen mode

The static keyword

In Java, static simply implies that the field or method is not going to change its value or behavior across all the instances of an object.

In other words, that means that we can call it without instantiating the object we are using.

So, if we define a CurrencyConverter:

class CurrencyConverter {

    public static String EUR = "€";

    public static double convertDollarsToEuros(double amountInDollars) {
        double rate = 0.90; 
        return amountInDollars * rate;
Enter fullscreen mode Exit fullscreen mode

We can then call the convertDollarsToEuros method without declaring any instance of the class:


CurrencyConverter converter = new CurrencyConverter();

>>> 4.887
>>> 4.887
Enter fullscreen mode Exit fullscreen mode

In the same way, we can call the static variable EUR without instantiating the object and, unlike final variables, we can even modify it.

But that we can do it doesn't mean that we should, as its considered bad practice to modify static variables.

That's why more often than not, static variables are also final:

public static final String EUR = "€"
Enter fullscreen mode Exit fullscreen mode

Hopefully, we now understand a little bit better the differences between those two keywords as well as when to use them.

Suggestions and constructive criticism are always welcome!

Top comments (0)

🌚 Life is too short to browse without dark mode