• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static Initializers

 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why doesn't this work:

I get a cannot resolve symbol error.
But this code works fine:

Thanks for your help.
Joe
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static initializers can only initialize static things. In the first example int i was not static, therefore it would need to be initialized for each instance of the class that gets created. Since the static initializer only runs on class load time, that is not possible to do. You could have left the declaration inside the initializer and just added the modifier static and it would have worked.
 
Joseph Russell
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


You could have left the declaration inside the initializer and just added the modifier static and it would have worked.


Actually I tried that it didn't work either. Here's the code and the error message.

Compiler output:
illegal start of expression
static int i
^
cannot resolve symbol
System.out.println( "Static code: i = " + i++ );
^
cannot resolve symbol
System.out.println( "main: i = " + i++ );
^
3 errors
Joe
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Joseph Russell:
[B]Why doesn't this work:

I get a cannot resolve symbol error.
But this code works fine:

Thanks for your help.
Joe[/B]


i is local to the static block and local variables cannot have any modifiers hence stati c int i will never work.Main wont be able to identify i unless you define it outside block.
You can only initialise it there and hence the name static intilizer.
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, Sandeep is right. I should have compiled that code before making that statement.
[This message has been edited by Cindy Glass (edited March 16, 2001).]
 
Joseph Russell
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess I just assumed that a static initializer would automatically make the int I was declaring static. Good thing I played with the code and found I was wrong. Thanks for helping me understand it all better.
Joe
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i ran the code with " int i=9" within the static initializer. it compiled and ran perfectly which means WE CAN INITIALIZE AN INSTANCE VARIABLE IN A STATIC CODE
it seems that one a static initilaizer cant do is to declare a static variable.
 
mansoor iqbal
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the code i ran
 
mansoor iqbal
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry.....my mistake..i didnt add the println thing in the main method...when i did..i got the same error.....sorry again!!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic