Explain what really happening when you put final in a class Like I'm Five
Valentin Silvestre

Valentin Silvestre @vasilvestre

About: French PHP developer. Get in touch with me ! https://www.linkedin.com/in/v-silvestre/

Location:
Lille - France
Joined:
Jul 7, 2017

Explain what really happening when you put final in a class Like I'm Five

Publish Date: Jun 29 '18
14 5

Variable change to constant ?
And what about function ?

My question is about what happen deep in the langage. (PHP or java, whatever)

Comments 5 total

  • Matheus Mohr
    Matheus MohrJun 29, 2018

    I guess you can either ask for a "like I'm five" explanation, or ask what happens deep in the language :P

    "final" means that thing doesn't change, simple as that.

    As for a deeper explanation considering Java, "final" tells the compiler that this element can be accessed/used without all the usual safe checking, allowing it to use such elements in multi-threaded processes without needing to synchronize it.

    There's a great answer here: stackoverflow.com/questions/273695...

    • K
      KJul 1, 2018

      Does it prevent extension?

      • Matheus Mohr
        Matheus MohrJul 1, 2018

        Yes, on it's own, the final modifier prevents a class from being extended. There are some workarounds for that, like delegating the final class calls through another (Foo is final, so you create Bar with the same interface as Foo, make it redirect every call to Foo's methods, and then extend Bar when you need. This is specially useful when you have absolutely no way of changing the code and still want to reuse it's functionalities)

  • Bertil Muth
    Bertil MuthJul 1, 2018

    I wrote a post with code examples that explains what final does to classes concretely.

Add comment