DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Java
  4. Static Variables and Compilers

Static Variables and Compilers

Pointing out an inconvenience in Java that really shouldn't be there anymore.

Ram Lakshmanan user avatar by
Ram Lakshmanan
CORE ·
Mar. 08, 17 · Tutorial
Like (8)
Save
Tweet
Share
10.20K Views

Join the DZone community and get the full member experience.

Join For Free

I am a long time Java programmer, almost 20 years now. I love this language a lot, and 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 the above code, the Java Compiler will raise an compilation error: "illegal forward reference". This is because the 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 for 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, function calls are resolved at runtime, whereas static finals are resolved at compile time. That's what causing this inconvenience. 

Advancements in technology have been progressing at an unbelievable pace. Applications are getting smarter and smarter. Applications are getting into the human mind and are starting to understand how a human is thinking and providing solutions accordingly. In this age of rapid advancement, there are a few things which are frozen in time. One of them is the sequencing of static variables in the Java language. Time for a change?

Java (programming language) Java compiler application dev Sort (Unix) Snippet (programming) Programmer (hardware)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How and Why You Should Start Automating DevOps
  • Top 5 PHP REST API Frameworks
  • Memory Debugging: A Deep Level of Insight
  • Core Machine Learning Metrics

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: