Does this definition make sense ?
private static final String string = "Constant string";
I'm a beginner and I don't understand the difference between final and static...
It makes sense, yes. This is how constants are defined in Java.
final
means that the variable cannot be reassigned - i.e. this is the only value it can havestatic
means that the same value is accessible from each instance of the class (it also means it can be accessed even without an instance of the class where it is declared)(private
here means this constant is accessible only to the current class (all of its instances and its static methods))
Yes, it makes sense - this is how constants are usually defined in Java.
final
means that you cannot change the variable to a different String.
static
means that there is only one copy of the reference, shared between different objects of that class. (reference)
Normal code-convention is that constants are named in uppercase with underscores between words, so I would say:
private static final String CONSTANT_STRING = "Constant string";
is more common.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With