Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9158465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:15:09+00:00 2026-06-17T13:15:09+00:00

When going through the JLS 8.3.2.3 I wasn’t able to understand the following code.

  • 0

When going through the JLS 8.3.2.3 I wasn’t able to understand the following code.

class Z {
static { i = j + 2; }
static int i, j;
static { j = 4; }
}

The code is resulting in the error Cannot reference a field before it is defined

But if I change the code to

class Z {
static { i = 2; }
static int i, j;
static { j = 4; }
}

The code is getting compiled. But in both the cases the variable definition is after the initialization block. What is the mystery behind this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:15:11+00:00Added an answer on June 17, 2026 at 1:15 pm

    You can assign to a value earlier than its declaration – you just can’t read it. So this fails too:

    static { System.out.println(j + 2); }
    static int j;
    

    Whereas this is fine:

    static { j = 5; }
    static int j;
    

    One of the four conditions in section 8.3.2.3 for an invalid usage is:

    • The usage is not on the left hand side of an assignment.

    (The double-negatives in that section are making my head hurt, but I think it relevant!)

    To be honest, that part of the spec is one of the worst I’ve seen – it’s really unclear. But the upshot is that you can assign but not read 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Going through the auto generated code of YII for a Model class, I understand
While going through Effective C++ (by Scott Meyers), I came across the following code
Going through this article class ThreadTest { static void Main() { Thread t =
Going through K&R I saw the following code snippet of a function, strcopy, which
Going through EASTL, I stumbled across a peculiar line of code. The following link
Going through Javascript documentation, I found the following two functions on a Javascript object
Just going through the sample Scala code on Scala website, but encountered an annoying
While going through 6.4 Packages section of python manual I came across the following
While going through university and from following the development of SO, I've heard a
While going through a Zend tutorial , I came across the following statement: Note

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.