Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static final?

 
Biby Thoms
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. you are right.
 
Nilesh Patel
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thing answer 2 & 3 both are correct
 
elangomaran kathirvel
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nilesh,

i think answer 3 is not correct. I believe you need to intitialize final variable when you declare.
 
Aiglee Castillo
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic