• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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.
 
Water! People swim in water! Even tiny ads swim in water:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic