• 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

Doubt in forward referencing

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source: http://www.javablackbelt.com/


How come i don't get a Compiler error complaining about forward referencing? Instead code compiles and prints 0. The value that is printed is it the default value of i or j?
[ April 13, 2008: Message edited by: sridhar row ]
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

there is no problem of "forward referecing" (by the way, i think
this kind of problem doesn't exist in java), because the instance
variables definition lines :
private int i = giveMeJ();
private int j = 10;
are in fact executed after the call to the super() constructor.
Here , there is no constructor, so you got the default constructor, that
is to say :

public AQuestion() {
super();
}
You also have to remember that instance variables always have default values, they are :
* 0 for numeric types
* null for reference types

So with the instance variables definition lines, we get :
public AQuestion() {
super();
i = giveMeJ();
j = 10;
}
And knowing that the initial values of j is 0, you have the full explanation about this code spinnet.

Hope it helps.

Gilles

Remark : if you switch the definition lines order, you'll get '10' printed,
which makes sense, because the initialisation instructions are processed in
the definition order.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This situation is also addressed here.

http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.3.2.3
 
sridhar row
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Gilles i got what you are saying.

Keith can you explain this a little bit more. I don't quite get it. Thanks.

The declaration of a member needs to appear textually before it is used only if the member is an instance (respectively static) field of a class or interface C and all of the following conditions hold:

* The usage occurs in an instance (respectively static) variable initializer of C or in an instance (respectively static) initializer of C.
* The usage is not on the left hand side of an assignment.
* The usage is via a simple name.
* C is the innermost class or interface enclosing the usage.

 
reply
    Bookmark Topic Watch Topic
  • New Topic