Java static and final, what are they and how to use them
Tags: java, begginers
Published: 2020-01-21 22:11:09

---
title: Java static and final, what are they and how to use them
published: true
description: 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.
tags: #java #begginers
---

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:

```java
	class Car {
		public final int numberOfWheels = 4; 
	}
```
Even if we wanted to modify this value, the compiler would throw an error:
```java

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

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

```

## Final methods

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

```java
class Car {

    public final int getNumberOfWheels() {
        return 4;
    }
}

class Sedan extends Car {

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

```
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: 
```java
final class Pear {
    private double weight;
    private String color;
}

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

}
```

# 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:
```java
class CurrencyConverter {
                
    public static String EUR = "€";

    public static double convertDollarsToEuros(double amountInDollars) {
	    double rate = 0.90; 
	    return amountInDollars * rate;
    }
}
```

We can then call the `convertDollarsToEuros` method without declaring any instance of the class:
```java
System.out.println(CurrencyConverter.convertDollarsToEuros(5.43D));
			
CurrencyConverter converter = new CurrencyConverter();
System.out.print(converter.convertDollarsToEuros(5.43D));

>>> 4.887
>>> 4.887
```

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:

```java
public static final String EUR = "€"
```

#

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!

#