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 567759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:07:03+00:00 2026-05-13T13:07:03+00:00

I came across these questions recently, and could not find the answer on StackOverflow;

  • 0

I came across these questions recently, and could not find the answer on StackOverflow;

  1. In what order are Java class variables initialised?
  2. And the somewhat related question, could re-ordering the variables change class behaviour?
  3. Why?

As suggested on Meta I will be posting my answer to this question.

  • 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-13T13:07:03+00:00Added an answer on May 13, 2026 at 1:07 pm

    In Java, class variables are initialised in the following order:

    1. Static variables of your superclasses
    2. All static variables of this class are set to their default values.
    3. Static variables, and static initialisation blocks, in declaration order.
    4. Instance variables of your superclasses
    5. All instance variables of this class are set to their default values.
    6. Instance variables, and instance level initialisation blocks, in declaration order

    1 & 2 are only done the very first time that a class is instantiated.

    So, given the following code:

    class Test
      extends TestSuper
    {
      final int ti1;
      final int ti2 = counter ++;
      { ti1 = counter ++; }
      static final int ts1;
      static final int ts2 = counter ++;
      static { ts1 = counter ++; }
    
      public static void main(String[] argv) {
        Test test1 = new Test();
        printTest(test1);
        Test test2 = new Test();
        printTest(test2);
      }
      private static void printTest(Test test) {
        System.out.print("ss2 = " + test.ss2);
        System.out.print(", ss1 = " + test.ss1);
        System.out.print(", ts2 = " + test.ts2);
        System.out.println(", ts1 = " + test.ts1);
        System.out.print("si2 = " + test.si2);
        System.out.print(", si1 = " + test.si1);
        System.out.print(", ti2 = " + test.ti2);
        System.out.println(", ti1 = " + test.ti1);
        System.out.println("counter = " + test.counter);
      }
    }
    
    class TestSuper
    {
      static int counter = 0;
      final int si1;
      final int si2 = counter ++;
      { si1 = counter ++; }
      static final int ss1;
      static final int ss2 = counter ++;
      static { ss1 = counter ++; }
    }
    

    Then we get the following output:

    ss2 = 0, ss1 = 1, ts2 = 2, ts1 = 3
    si2 = 4, si1 = 5, ti2 = 6, ti1 = 7
    counter = 8
    ss2 = 0, ss1 = 1, ts2 = 2, ts1 = 3
    si2 = 8, si1 = 9, ti2 = 10, ti1 = 11
    counter = 12
    

    From this output we can see that the fields are initialised in the order specified in the list.

    Now, as to the second question, can re-ordering the fields change the class behaviour. Yes, by re-ordering the fields you change the initialisation order of the fields. Now, in the specific case where all of the fields are independent, this won’t affect the observed behaviour, however whenever the fields are not independent, for example in the above code, then re-ordering the fields could change their initialised values.

    For example, if the three lines:

      static final int ss1;
      static final int ss2 = counter ++;
      static { ss1 = counter ++; }
    

    are changed to:

      static final int ss1;
      static { ss1 = counter ++; }
      static final int ss2 = counter ++;
    

    Then the output would change to:

    ss2 = 1, ss1 = 0, ts2 = 2, ts1 = 3
    si2 = 4, si1 = 5, ti2 = 6, ti1 = 7
    counter = 8
    

    That is, ss2, and ss1 would change values.

    The reason for this is that this behaviour is specified in the Java Language Specification.

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

Sidebar

Related Questions

No related questions found

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.