I am a long time Java programmer, now almost 20 years. Since I love this language a lot, I would like to share an observation.

Consider the below code snippet:

public static final String HELLO_WORLD = "Hello" + WORLD;
public static final String WORLD = "World";

When you try to compile above code, Java Compiler will raise a compilation error: “illegal forward reference.”

It’s because compiler expects the static variable ‘WORLD’ to be defined first before referencing it. In order to compile successfully, the code has to be modified to:

public static final String WORLD = "World";
public static final String HELLO_WORLD = "Hello" + WORLD; 

It would be convenient to Java developers if this sort of sequencing can be eliminated. Java developers are already used to this sort of flexibility when they define methods. When you define java methods, you don’t have to follow any sequence. Methods defined at the bottom of the class can be referenced by methods in top of the class. In Java language, function calls are resolved at runtime, where as static finals are resolved in compile time – that’s what causing this inconvenience.

Advancement in technology has been progressing at an unbelievable phase making application development smarter and intelligent. Applications are getting into human’s mind and starting to read what a human is thinking and serving solutions accordingly. In this rapid advancement age, there are few things which are frozen in time. One of them is the sequencing of static variables in the Java language. Maybe time to enhance?


Add yours

  1. Static final are different from function calls. Function calls are resolved at runtime, where static final are resolved at compile time. Compiler could inline reference to static final at compile-time (replace that reference with value).

    Therefore, for functions it is possible to have a() calling b() and b() calling a().

    I think that for simple case described above, compiler could be smart enough to reorder declarations to resolve forward references.

    But, for readability purposes I would rather declare simple constant first, then composite constants later anyway. That makes code simpler to read, understand and reason about.

    Finally, please also remember about gotha when referring to final fields from another classes/packages. It may cause bugs that are extremely tricky to track down.

    See for exact problem description

  2. I think function have the run time binding for other purposes (polymorphism, etc ) as well, but in case of static final variables, it is worth to have a run time biding to resolve forward reference issue. any way readability should be the key here to have compile time reference check.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at

Up ↑

%d bloggers like this: