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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:55:10+00:00 2026-06-10T00:55:10+00:00

I am wondering what’s the purposes of using empty block. For example, static{ int

  • 0

I am wondering what’s the purposes of using empty block. For example,

    static{
        int x = 5;
    }

    public static void main (String [] args){

          int i = 10;

          {
               int j = 0 ;
               System.out.println(x);  // compiler error : can't find x ?? why ??
               System.out.println(i);  // this is fine
          }
          System.out.println(j); //compiler error :  can't find j

    }

Can someone explains

  1. In what situation that we would want to use empty block.
  2. Are all the variables inside that empty block still goes on stack ?
  3. Why couldn’t it access the static variable x ?
  • 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-10T00:55:10+00:00Added an answer on June 10, 2026 at 12:55 am
    1. The block that you are showing in your post is not an empty block, it is a static initializer. It is used to provide non-trivial initialization for static variables of your class
    2. Local variables that you use during initialization go on stack, except for objects that you allocate from the heap
    3. You cannot access static variable x because you did not declare it. Instead, you declared a local variable x inside a static initializer.

    If you would like to make x a static variable and then initialize it in a static initialization block, do this:

    private static int x;
    static {
        x = 5;
    }
    

    In trivial cases like this, a straightforward initialization syntax works best:

    private static int x = 5;
    

    Initializer blocks are reserved for more complex work, for example, when you need to initialize structures using a loop:

    private static List<List<String>> x = new ArrayList<List<String>>();
    static {
        for (int i = 0 ; i != 10 ; i++) {
            x.add(new ArrayList<String>(20));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Wondering what the difference is between the following: Case 1: Base Class public void
wondering if some of you know what javascript framework facebook is using ? Thanks
wondering how I can replace all special chars on my string like: hello this
Wondering if anyone has had any experience using retina assets with the NativeControls iPhone
Wondering if this is possible or something with this effect. public class MyModel {
Wondering if there is a fast way, maybe with linq?, to convert a Dictionary<string,string>
Wondering why the example below doesn't work? <a id=load_list href=#>Load the list</a> <ul></ul> <script
Wondering if anyone can tell me how to use the Apache Lucene method 'valueOf(String)'
Wondering about using gzipped resources for my iOS app. The idea is to unzip
Wondering if anybody out there has any success in using the JDEdwards XMLInterop functionality.

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.