Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of final in java

Tags:

java

final

I was wondering what the difference is between

public final type attribute_name;

and

private type attribute_name;
public type getA_name() {
  return attribute_name;
}

Basically I want to make an attribute read-only, so it can't change after it has been initialized.
Do I make it public final, or do I make it private, and only make it accesible through a get method (without a set method)?

like image 553
Don Avatar asked Apr 16 '26 08:04

Don


1 Answers

When it's not final but private, the class itself is able to change the value.

like image 163
halloei Avatar answered Apr 18 '26 20:04

halloei



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!