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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:10:04+00:00 2026-06-11T11:10:04+00:00

Possible Duplicate: Java static class initialization Why is the string variable updated in the

  • 0

Possible Duplicate:
Java static class initialization

Why is the string variable updated in the initialization block and not the integer(even though the block is written first)

class NewClass
{
    static 
    {
       System.out.println(NewClass.string+" "+NewClass.integer);
    }

    final static String string="static";
    final static Integer integer=1;

    public static void main(String [] args)//throws Exception
    {
    }
}

My output is

static null

P.S:Also noticed that string variable initialization happens before the block only when i insert the final modifier. why is that?why not for integer as well?I have declared it as final static too

  • 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-11T11:10:06+00:00Added an answer on June 11, 2026 at 11:10 am

    From section 12.4.2 of the JLS, snipped appropriately:

    The procedure for initializing C is then as follows:

    • Then, initialize the final class variables and fields of interfaces whose values are compile-time constant expressions (§8.3.2.1, §9.3.1, §13.4.9, §15.28).

    • Next, execute either the class variable initializers and static initializers of the class, or the field initializers of the interface, in textual order, as though they were a single block.

    So for non-compile-time-constants, it’s not a case of "all variables" and then "all static initializers" or vice versa – it’s all of them together, in textual order. So if you had:

    static int x = method("x");
    
    static {
        System.out.println("init 1");
    }
    
    static int y = method("y");
    
    static {
        System.out.println("init 2");
    }
    
    static int method(String name) {
        System.out.println(name);
        return 0;
    }
    

    Then the output would be:

    x
    init 1
    y
    init 2
    

    Even making x or y final wouldn’t affect this here, as they still wouldn’t be compile-time constants.

    P.S:Also noticed that string variable initialization happens before the block only when i insert the final modifier.

    At that point, it’s a compile-time constant, and any uses of it basically inlined. Additionally, the variable value is assigned before the rest of the initializers, as above.

    Section 15.28 of the JLS defines compile-time constants – it includes all primitive values and String, but not the wrapper types such as Integer.

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

Sidebar

Related Questions

Possible Duplicate: java operator ++ problem public class A { public static void main(String[]
Possible Duplicate: Is Java pass by reference? public class myClass{ public static void main(String[]
Possible Duplicate: Java inner class and static nested class What are the uses of
Possible Duplicate: Is a Java static block equivalent to a C# static constructor? Is
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: 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
Possible Duplicate: Difference between static class and singleton pattern? Which is better in Java,
Possible Duplicate: static in the main class java and non static in constructor i

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.