Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Xavi
Xavi

Posted on • Edited 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 betweenstatic andfinal, 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.

Thefinal keyword

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

Final variables

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

classCar{publicfinalintnumberOfWheels=4;}
Enter fullscreen modeExit fullscreen mode

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

CarmyCar=newCar();myCar.numberOfWheels=1;>>>ThefinalfieldCar.numberOfWheelscannotbeassigned
Enter fullscreen modeExit fullscreen mode

Final methods

Final methods cannot be overridden by any subclasses. Assume the classCar and the classSedan:

classCar{publicfinalintgetNumberOfWheels(){return4;}}classSedanextendsCar{// This won't work because the method getWeight is final!@OverridepublicdoublegetNumberOfWheels(){return3;}}
Enter fullscreen modeExit 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:

finalclassPear{privatedoubleweight;privateStringcolor;}// This won't work because the Pear class is final!classMagicalPearextendsPear{}
Enter fullscreen modeExit fullscreen mode

Thestatic 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:

classCurrencyConverter{publicstaticStringEUR="€";publicstaticdoubleconvertDollarsToEuros(doubleamountInDollars){doublerate=0.90;returnamountInDollars*rate;}}
Enter fullscreen modeExit fullscreen mode

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

System.out.println(CurrencyConverter.convertDollarsToEuros(5.43D));CurrencyConverterconverter=newCurrencyConverter();System.out.print(converter.convertDollarsToEuros(5.43D));>>>4.887>>>4.887
Enter fullscreen modeExit fullscreen mode

In the same way, we can call the static variableEUR without instantiating the object and,unlike final variables, we can evenmodify 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:

publicstaticfinalStringEUR="€"
Enter fullscreen modeExit 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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Java backend engineer
  • Location
    Barcelona
  • Education
    Electronics engineering
  • Work
    Software engineer
  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp