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

  • Home
  • SEARCH
  • 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 1063429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:46:58+00:00 2026-05-16T18:46:58+00:00

public class Main { static int x = Main.y; // static int x =

  • 0
public class Main {

    static int x = Main.y;
//  static int x = y; //Not allowed; y is not defined
    static int y = x;
    public static void main(String[] args) {
        System.out.println(x);//prints 0
    }
}

How come I am allowed to use y trough the class, but not directly?

When is y defined?

  • 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-05-16T18:46:59+00:00Added an answer on May 16, 2026 at 6:46 pm

    The precise rules governing forward reference to class variables are described in the section §8.3.2.3 of the JLS:

    8.3.2.3 Restrictions on the use of Fields during Initialization

    The declaration of a member needs to
    appear textually before it is used
    only if the member is an instance
    (respectively static) field of a class
    or interface C and all of the
    following conditions hold:

    • The usage occurs in an instance (respectively static) variable
      initializer of C or in an instance
      (respectively static) initializer of
      C.
    • The usage is not on the left hand side of an assignment.
    • The usage is via a simple name.
    • C is the innermost class or interface enclosing the usage.

    A compile-time error occurs if any of
    the four requirements above are not
    met.

    This means that a compile-time error
    results from the test program:

      class Test {
          int i = j;  // compile-time error: incorrect forward reference
          int j = 1;
      }
    

    whereas the following example compiles
    without error:

      class Test {
          Test() { k = 2; }
          int j = 1;
          int i = j;
          int k;
      }
    

    even though the constructor
    (§8.8) for Test refers to the
    field k that is declared three lines
    later.

    These restrictions are designed to
    catch, at compile time, circular or
    otherwise malformed initializations.
    Thus, both:

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

    and:

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

    result in compile-time errors.
    Accesses by methods are not checked in
    this way, so:

    class Z {
      static int peek() { return j; }
      static int i = peek();
      static int j = 1;
    }
    class Test {
      public static void main(String[] args) {
          System.out.println(Z.i);
      }
    }
    

    produces the output:

    0
    

    because the variable initializer for i
    uses the class method peek to access
    the value of the variable j before j
    has been initialized by its variable
    initializer, at which point it still
    has its default value (§4.12.5).

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

Sidebar

Related Questions

public class Foo { public static void main(String[] args) { float f; System.out.println(f); }
public class Sample { public static void main(String[] args) { int year = 2012;
public class I { public static void main(String args[]) { int i=0,j=1,k=2; i=i++ -
public class MultiplicationTable { public static void main (String[]a){ int[] x; x = new
public class Main { public static void main(String[] args){ Class2 class2Object = new Class2();
package GC; import java.util.Scanner; public class main { public static void main(String args[]) {
public class Test { public static void main(String[] args) { DemoAbstractClass abstractClass = new
public class b { public static void main(String[] args) { byte b = 1;
Here is my code: public class Main { public static void main(String[] args) {
In the following program class ZiggyTest2 { public static void main(String[] args){ double x

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.