| Author |
Q based on Dan's exam
|
mari krishna
Greenhorn
Joined: Feb 13, 2003
Posts: 10
|
|
Hi there, In one of Dan's exams, its says "A blank final variable that is static must be definitely assigned in a static initializer. " marked true. but when i tried on code, didnt seem to work static { static final int i; } gives an error saying - illegal start of expression static final int i; pls help! thanks in advance mari.
|
 |
Richard Jensen
Ranch Hand
Joined: May 14, 2003
Posts: 67
|
|
Originally posted by mari krishna: gives an error saying - illegal start of expression static final int i;
I think you need to declare the static final variable separately from the static initializer block:
|
Richard
N 37 33 W 122 18
|
 |
mari krishna
Greenhorn
Joined: Feb 13, 2003
Posts: 10
|
|
Originally posted by Richard Jensen: gives an error saying - illegal start of expression static final int i; <hr></blockquote> I think you need to declare the static final variable separately from the static initializer block:
tried that too compiler error says - variable i might not have been initialized.
|
 |
Richard Jensen
Ranch Hand
Joined: May 14, 2003
Posts: 67
|
|
My compiler does not give me an error or warning on: If I comment out the "i=57" line, then I DO get that warning. Is there anything that is conditional in your static block that might cause the initialization to be skipped?
|
 |
Brian Joseph
Ranch Hand
Joined: May 16, 2003
Posts: 160
|
|
|
Ah, cool, I never tried to actually declare variables within an initialization block or static init block. That's probably why it's called an "initialization" block, because you are only allowed to initialize variables which are already delcared?
|
 |
Anupam Sinha
Ranch Hand
Joined: Apr 13, 2003
Posts: 1088
|
|
Originally posted by Brian Joseph: Ah, cool, I never tried to actually declare variables within an initialization block or static init block. That's probably why it's called an "initialization" block, because you are only allowed to initialize variables which are already delcared?
Consider this : Notice one more thing you can get away by not intializing a final variable. This is also possible in methods as well. Till the time you don't use it the compiler doesn't care. I guess this is because the variable can only be used in the block or the method. So in case you are not using it in the block or method it does not complain. But a class variable can be used in a different class, than it was declared. [ June 22, 2003: Message edited by: Anupam Sinha ]
|
 |
Prashant More
Greenhorn
Joined: Jun 19, 2003
Posts: 1
|
|
"A blank final variable that is static must be definitely assigned in a static initializer. " this means you can initialize final static variables only in static block and not in constructors, where as you can initialize normal final variables in constructor code: class Test { static final int i; static { i=1;//right place to initialize } Test(){ //i=1;//will give error } public static void main(String[] args) { System.out.print(new Test().i+""); } }
|
 |
Anupam Sinha
Ranch Hand
Joined: Apr 13, 2003
Posts: 1088
|
|
this means you can initialize final static variables only in static block and not in constructors, where as you can initialize normal final variables in constructor
You can also intialize a final static variable also at the time of declaration. I think that you may already be aware of this but in the SCJP words like "may" and "will" may have totally different meanings.
|
 |
 |
|
|
subject: Q based on Dan's exam
|
|
|