• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Doubt in Static initialization

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I need help in understanding the following code from JQPlus:


I am trying to use the CLASS_GUID as follows:


Now , if I rewrite the line 0 as
static final String CLASS_GUID;
Once that is done ...My questions:
1) uncomment line 2 and it gives cannot assign value to final variable ...Why?
2) UnComment line 1 , comment line 2 it gives no error.
3) Comment line 1 and 2 and it gives the variable CLASS_GUID might not be initialized....Why? Arent the static instance variables initialized? For a second if I think they werent then Why did it not allow me to do the initialization in line 1?

Please explain...

thanks,
Megha
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Megha,


The facts behind constants:

1- constant is static and final
it must be initialized where it is declared or in static block, no other way. And ofcourse once initialized can't be re- initialized.
2- final member must be initialized where declared or in block but must be defined before constructor completes.


And some code:





Thanks,
cmbhatt
[ April 28, 2007: Message edited by: Chandra Bhatt ]
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry I am still not clear...

Does it mean that ...

1) Constants are not given default values by compiler.
2) Constants can't be initialized in constructors.

Thanks,
Megha
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by megha joshi:
Sorry I am still not clear...

Does it mean that ...

1) Constants are not given default values by compiler.
2) Constants can't be initialized in constructors.

Thanks,
Megha



Megha, see my edited post above, where I have added some code
to probe to these facts.

1- You know constants =(static+final)
2- Compiler never assigns default values to them, you have to
explicitly assign them when declared or in static block.
3- Only final is not meant constant.
4- One more thing, final or static final is also known as "blank final";

5-final (saying only final) must be initialized before constructor completes (means either in the block codes that executes before constructor or in the constructor itself).


Thanks,
cmbhatt
[ April 28, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 363
Firefox Browser Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by megha joshi:
Hi all,

I need help in understanding the following code from JQPlus:


I am trying to use the CLASS_GUID as follows:


Now , if I rewrite the line 0 as
static final String CLASS_GUID;
Once that is done ...My questions:
1) uncomment line 2 and it gives cannot assign value to final variable ...Why?
2) UnComment line 1 , comment line 2 it gives no error.
3) Comment line 1 and 2 and it gives the variable CLASS_GUID might not be initialized....Why? Arent the static instance variables initialized? For a second if I think they werent then Why did it not allow me to do the initialization in line 1?

Please explain...

thanks,
Megha



static blocks exist to initialize static member variables.
final variables can't be assigned new values once assigned.

If you still have questions, read their descriptions/definitions and better think. It will definitely help you alot. Heard burning into brain? That's what Im asking you to do!
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Chandra and Faisal:

Following is what I am burning into my brain

a) Constants and final variables are not given default values by compiler.
b) They are to be intialized before constructor code starts( I think it shoundn't be " ends " as I was not allowed to initialize them in constructor) ...that is either while declaring them or in static initialization blocks(in case of just final, its instance initialization blocks)..

Thanks!
Megha
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wait Megha............................
You are supposed to burn little corrupt data on you brain's
CD!!!


Megha says,
b) They are to be intialized before constructor code starts( I think it shoundn't be " ends " as I was not allowed to initialize them in constructor) ...that is either while declaring them or in static initialization blocks(in case of just final, its instance initialization blocks)..




Review the following code:


Phew!!!
... Now you can click the "Burn" button.

Regards,
cmbhatt
[ April 28, 2007: Message edited by: Chandra Bhatt ]
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it

Burn:
-----------------------------------------------------------------
When Compiler encounters "final" modifier for variable it doesnt give default value. So....
b) Final static(constant) variables must be initialized before constructor starts.
i ) in declaration OR
ii) in static intialization blocks

c) Final variables must be intialized before constructor ends.

i ) in declaration OR
ii) in intialization blocks OR
iii) in constructor


---------------------------------------------------------------------------

Thanks for your patience Chandra

[ April 28, 2007: Message edited by: megha joshi ]
[ April 28, 2007: Message edited by: megha joshi ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic