This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Q based on Dan's exam

 
mari krishna
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
mari krishna
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 160
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1090
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 1090
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic