• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

static final?

 
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
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. you are right.
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thing answer 2 & 3 both are correct
 
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.
 
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.
 
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 ]
 
You can't expect to wield supreme executive power just because
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic