Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • 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

New question for tax day

 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The above class will give a compile error for an illegal forward reference because we are accessing b before we have defined it.
If we change:
static int a = b;
to
static int a = forwardReference();
what will the output be?
[ April 15, 2003: Message edited by: Thomas Paul ]
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With a = forwardReference(); we will be able to fool the compiler, so it compiles fine.
How ever when you run it, when you call forwardReference(), the static variable b has not yet been initialized. So the return b; statement returns the defualt value 0.
And that is what gets assigned to a.
So the print statement in the main prints
TestForward.a as 0.

Monisha.
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct! Since we have disguised our forward reference, the compiler lets it go and we get, probably, an unexpected result.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does it mean that
static method are initialized before static variables?
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Does it mean that static method are initialized before static variables?


Charles, methods never get initialized.
It is the memeber variables that are intialized using initializer expressions and initializer blocks. Approriate method simply get invoked when there is a call for it in an initializer expressions or initializer blocks.
Static initializer expressions and static initializer blocks get executed only once when the class is loaded, in the order they appear in the class.
Instance initializer expressions and instance initializer blocks get executed every time a new instance is created, in the order they appear in the class.
 
Villains always have antidotes. They're funny that way. Here's an antidote disguised as a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic