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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:12:10+00:00 2026-06-15T05:12:10+00:00

Possible Duplicate: Java static class initialization in what order are static blocks and static

  • 0

Possible Duplicate:
Java static class initialization
in what order are static blocks and static variables in a class executed?

When I run this code the answer is 1, I thought it would be 2.
What is the order of initialization and the value of k in each step?

public class Test {

    static {k = 2;}
    static int k = 1;

    public static void main(String[] args) {
        System.out.println(k);
    }
}

Edit 1: As a follow up to “k is set to default value” then why this next code doesn’t compile? Theres an error “Cannot reference a field before it’s defined”.

public class Test {

    static {System.out.println(k);}
    static int k=1;

    public static void main(String[] args) {
        System.out.println(k);
    }
}

Edit 2: For some unknow to me reason it^ works when instead of “k” its “Test.k”.

Thanks for all the answers. this will sufice 😀

  • 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-15T05:12:12+00:00Added an answer on June 15, 2026 at 5:12 am

    They are executed in the order that you write them. If the code is:

    public class Test {
    
        static int k = 1;
        static {k = 2;}
    
        public static void main(String[] args) {
            System.out.println(k);
        }
    
    }
    

    then the output becomes 2.

    The order of initialization is: ..the class variable initializers and static initializers of the class…, in textual order, as though they were a single block.

    And the values (for your code) are: k = 0 (default), then it’s set to 2, then it’s set back to 1.

    You can check that it’s actually set to 2 by running the following code:

    private static class Test {
    
        static {
            System.out.println(Test.k);
            k = 2;
            System.out.println(Test.k);
            }
        static int k = 1;
    
        public static void main(String[] args) {
            System.out.println(k);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Java: Global Exception Handler I thought of something like this: public static
Possible Duplicate: Java static class initialization Why is the string variable updated in the
Possible Duplicate: Java inner class and static nested class What are the uses of
Possible Duplicate: Getting the class name from a static method in Java When you
Possible Duplicate: Java inner class and static nested class An instance of a static
Possible Duplicate: Static initializer in Java I wondering what this static something (sorry it's
Possible Duplicate: Why won't this generic java code compile? Given the following code: import
Possible Duplicate: What is the difference between Class.this and this in Java I know
Possible Duplicate: Retain precision with Doubles in java import static java.lang.System.out; public class q2{
Possible Duplicate: Static nested class in Java, why? I know about the static fields

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.