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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:54:35+00:00 2026-06-02T03:54:35+00:00

I know that static blocks are run before anything. But here, what happens when

  • 0

I know that static blocks are run before anything. But here, what happens when B.test() is called? Order of execution and setting of values? Later, when b1 is set to null, still how b1.i evaluates to 20?

class  B
{    
     static int i;
     static {
         i = 20;
         System.out.println("SIB");
     }
     static int test() {  
         int i = 24;
         System.out.println(i);
         return i; 
     }
}

public class Manager {
    public static void main(String[] arg) {
         B.test();
         B b1 = null;
         System.out.println(b1.i);  
    }
}

The output is:

SIB
24
20
  • 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-02T03:54:38+00:00Added an answer on June 2, 2026 at 3:54 am

    i is static, so b1.i is equivalent to B.i. Just setting b1 to null doesn’t change any static variables.

    When B.test() is called, first the B class is loaded, and the static blocks are run. Next, B.test() creates a new method-local variable called i, which is completely different from B.i. This new, local i is printed and returned. No changes are made to B.i, and certainly not just because you created a new B object reference that was null — that would never have any effect on anything.

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

Sidebar

Related Questions

I know that when linking to multiple static libraries or object files, the order
I know that static const int x = 42; at namespace scope is equivalent
I know that the static objects in .Net managed world are loaded in Loader
We know that the classic range random function is like this: public static final
I know that lot of questions about HTML sanitizers have appeared in SO, but
I know that all of these will be compiled together into one file but
I know that similar question was asked several times, but still I can't make
I want my client class to run a thread that sends String information (order)
When should we use an uninitialized static final variable? I know that an uninitialized
I know that VB offers many things to control it : SDK, API, COM,

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.