Help coderanch get a
new server
by contributing to the fundraiser
  • 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

Illegal forward referencing

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why this example doesn't give an error of Illegal forward referencing? and how come output is 0 and not 10?

public class A118
{
private int i = giveMeJ();
private int j = 10;
private int giveMeJ()
{
return j;
}

public static void main(String args[])
{
System.out.println((new A118()).i);
}
}
 
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because j is not initialized when giveMeJ() is invoked by i.
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi sachin

that was a nice question ...even i get the same doubt ...
fnds,the variables are intialised before the constructor is called ..so when the method is invoked how come the compiler know the value of j....
i missing somewhere in my concepts about the instance variable intialisation.
can anybody plz explain this ....

thanks & regards

srikanth
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It has to do with the order in which in which a new instance gets created. The order is (roughly) as follows:

1. Memory is allocated for the object
2. Instance variables are initialized to their default values (in this case, i and j are initialized to 0 because that's the default for ints)
3. Constructor hierarchy is invoked for all parent objects (in this case, none)
4. Execute instance initializers (this is where i gets assigned to the result of giveMeJ() -- which returns 0 because j is still 0 -- and j gets assigned to 10)
5. Execute the rest of the constructor body

Of course, the order of the initialization matters (illegal forward referencing). For example, on my machine, the following code yields:

0
10
10



But since j *is* initialized to 0 before the method call, you won't get the illegal forward referencing error.

For more info, see section 12.5 of the JLS.

http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.5

See also this explanation of object initialization. The whole article is pretty good.

EDIT: Fixing code example, and added additional link.

[ August 18, 2005: Message edited by: Ryan Kade ]

[ August 18, 2005: Message edited by: Ryan Kade ]
[ August 18, 2005: Message edited by: Ryan Kade ]
 
Can you hear that? That's my theme music. I don't know where it comes from. Check under this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic