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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post 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();
	}    
}

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

By calling this(), you are initializing value in this constructor also. That is the point.

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests