This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Which answer is correct. class Widget { static int MAX; //1 static final String CLASS_GUID; // 2 Widget() { //3 } Widget(int k) { //4 } } 1.Add the following line just before //1 {MAX=111;CLASS_GUID="SS";} 2.Modify lines //1 and //2 as static int MAX=111;static final String CLASS_GUID="SS"; 3.Add the following line just after //2 static {MAX=111;CLASS_GUID="SS";}
It is a question from enthuware software. It says the answer is 1 But I got the error with the option 1. I believe anser is 2. Please advise
Thanks<br />Biby Thoms
wise owen
Ranch Hand
Joined: Feb 02, 2006
Posts: 2023
posted
0
Yes. you are right.
Nilesh Patel
Ranch Hand
Joined: Feb 02, 2006
Posts: 91
posted
0
I thing answer 2 & 3 both are correct
Nilesh Patel
SCJP 1.5 - 87%
elangomaran kathirvel
Greenhorn
Joined: Jan 24, 2005
Posts: 6
posted
0
Hi Nilesh,
i think answer 3 is not correct. I believe you need to intitialize final variable when you declare.
Regards,<br />Elangomaran
Aiglee Castillo
Greenhorn
Joined: Aug 10, 2006
Posts: 27
posted
0
Number 1 is wrong because you can't assign a value to MAX and CLASS_GUID without first declaring them.
Number 2 and 3 are right. I have done lots of times the Rules Roundup game and there is a question/answer that it says you can assign the value to a final variable after the declaration without problem.
Naseem Khan
Ranch Hand
Joined: Apr 25, 2005
Posts: 809
posted
0
Number 2 and 3 are right. I have done lots of times the Rules Roundup game and there is a question/answer that it says you can assign the value to a final variable after the declaration without problem.
To be more precise, if you have not initialized static final variable at the time of declaration, then how late you can initialize it?
Initialization in static initialization block is must in that case.
Similarly if variable is non-static and final, then they must be initialized in every overloaded constructor.