@[toc]

Final modified properties cannot be modified?

The final variable also requires an initial value

// Error!
public class TestDemo {
	public final int num;
}
Copy the code
// Error!
public class TestDemo {
	public final int num = 1;
	{
		num = 2; }}Copy the code

Let’s look at some examples that are correct

// This is the correct way to write
public class TestDemo {
	public final int num;
	{
		num = 1; }}Copy the code
// This is the correct way to write
public class TestDemo {
	public final int num;
	public TestDemo(a) {
		num = 1; }}Copy the code
// This is the correct way to write
public class TestDemo {
	public static final int num;
	static {
		num = 1; }}Copy the code

conclusion

Final modifier properties generally require an initial value. Those that do not need an initial value should be initialized ina code block or constructor! The property value cannot be modified after that