wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static final? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static final?" Watch "static final?" New topic
Author

static final?

Biby Thoms
Ranch Hand

Joined: Jan 10, 2006
Posts: 48
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
Yes. you are right.
Nilesh Patel
Ranch Hand

Joined: Feb 02, 2006
Posts: 91
I thing answer 2 & 3 both are correct


Nilesh Patel
SCJP 1.5 - 87%
elangomaran kathirvel
Greenhorn

Joined: Jan 24, 2005
Posts: 6
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
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
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.

Naseem
[ August 10, 2006: Message edited by: Naseem Khan ]

Asking Smart Questions FAQ - How To Put Your Code In Code Tags
 
wood burning stoves
 
subject: static final?
 
Similar Threads
Turn Based Strategy Games
Doubt in Static initialization
can any one let me know me the output???
Sample Code Problem
Threads - Bill Brodgen