File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes initialization doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "initialization doubt" Watch "initialization doubt" New topic

initialization doubt

Deep Chand
Ranch Hand

Joined: Dec 17, 2002
Posts: 133
The following program gives compilation error: 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?
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;

Karthik Balasubramanian
Ranch Hand

Joined: Dec 13, 2002
Posts: 43
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
Hope I am correct! I am also preparing for SCJP 1.4...
Java Veterans...Your comments please, if it is wrong.
Deep Chand
Ranch Hand

Joined: Dec 17, 2002
Posts: 133
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?
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
Also check out JLS Restrictions on the use of Fields during Initialization

[Blog] [Blogroll] [My Reviews] My Linked In
I agree. Here's the link:
subject: initialization doubt
It's not a secret anymore!