File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in Static initialization 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 "Doubt in Static initialization" Watch "Doubt in Static initialization" New topic
Author

Doubt in Static initialization

megha joshi
Ranch Hand

Joined: Feb 20, 2007
Posts: 206
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
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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 ]

cmbhatt
megha joshi
Ranch Hand

Joined: Feb 20, 2007
Posts: 206
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

Joined: Feb 28, 2007
Posts: 1707
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 ]
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

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

Joined: Feb 20, 2007
Posts: 206
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

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Feb 20, 2007
Posts: 206
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in Static initialization