Page 1 of 1

[HD Pg 215, Sec. 8.5.5 - final-variables-revisited]

Posted: Mon May 13, 2019 1:11 pm
by flex567
Since an instance variable can be used by other classes only after an object of this class is created, it follows that a instance final
variable can be initialized at the time of declaration, in any of the instance initializers, or in all of the constructors.

I don't think it needs to be initialized in all constructors?
In the last constructor there is just a call to the other constructor but no initialization in it.

Code: Select all

public class TestClass{
    
	final int value; //not initializing here
	
	{
		//not initializing value here either
	}
	
	TestClass(){
		value = 10;
	}
	
	TestClass(int x){
		value = x;
	}
	
	TestClass(int a, int b){ 
	this();
	}    
}

Re: [HD Pg 215, Sec. 8.5.5 - final-variables-revisited]

Posted: Mon May 13, 2019 8:28 pm
by admin
By calling this(), you are initializing value in this constructor also. That is the point.