• 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

initialization doubt

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following program gives compilation error:
Temp.java:22: illegal forward reference
static int j = t3*getValue(100);
^
1 error
I understand because "t3" is yet not defined. So compiler doesn't yet know about "t3".
However, if I change it to "static int j = getValue(100); //line 22" then it works fine and output
is "j = 101 t3 = 5"
How come compiler is happy for this? How the compiler knows about "t3", which is used
in getValue(int) method because I thought that statics are intialized/defined in the textual order.
Could anybody throw some light on this?
Thanks,
Deep
public class Temp {

Temp() {
System.out.println("j = " + j + " t3 = " + t3);
}


public static void main (String args[]) {

Temp t = new Temp();

}



private static int getValue(int i) {
System.out.println("getValue " + i);
return ++i+t3;
}

static int j = t3*getValue(100);
static int t3 = 5;

}
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Deep,
See...even when you remove t3 from definiton of j, the compiler,does not recognize t3. If it had known the value of t3=5, then the getValue method would have returned 106(++i+t3 = 101+5)...but you have got only 101. I think the compiler assumes the value of zero for t3.
Hope you know, multiple static initializers are legal in java and are executed in order when the class is loaded.
Just bring the statement "static int t3 = 5;" above the "static int j = t3*getValue(100);". Your code will work fine and you get the output as follows:
getvalue 100
j=530,t3=5
Hope I am correct! I am also preparing for SCJP 1.4...
Java Veterans...Your comments please, if it is wrong.
Thanks
Karthik
 
Deep Chand
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Kartik,
Thanks for the reply. However, I'm still confused (:
If t3 is initialized to 0 then why the compiler doesn't put that value in j = t3*getValue(100) expression???
When an object is created, I see the initialization as following steps(for a normal class that is not inherited from any user-defined class):
a) Class X is loaded into memory.
b) Static variables and initializers are run in the textual order in which they are defined(not static methods).
c) Class variables are initialized to 0 or their values (if defined at point of initialization)
d) Constructor is executed.
If this is correct then how come compiler knows about "static variable t3" in getValue(), when it has not seen yet?
Am I correct?
Thanks,
Deep
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also check out JLS 8.3.2.3 Restrictions on the use of Fields during Initialization
 
reply
    Bookmark Topic Watch Topic
  • New Topic