Compared to C #, Java final is like const or readonly

Interestingly, compared to C #, java final looks more like what? const or readonly?

+4
source share
2 answers

readonly , because, like in C #, you can only set final once, including inside the constructor.

+7
source

Interest Ask,

The Java final keyword implies several things:

  • You can assign a value only once
  • You must assign a variable in the constructor or as part of the declaration.

With # readonly keyword applies basically the same restrictions.

For completeness, let's look at const :

  • It can only be assigned once as part of an ad.
  • It is essentially static - there is only one of them for all N instances of your class

So - I would say that final is more like readonly.

- Dan

+8
source

All Articles