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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:39:31+00:00 2026-05-10T23:39:31+00:00

My question is about one particular usage of static keyword. It is possible to

  • 0

My question is about one particular usage of static keyword. It is possible to use static keyword to cover a code block within a class which does not belong to any function. For example following code compiles:

public class Test {     private static final int a;         static {         a = 5;         doSomething(a);     }     private static int doSomething(int x) {         return (x+5);     } } 

If you remove the static keyword it complains because the variable a is final. However it is possible to remove both final and static keywords and make it compile.

It is confusing for me in both ways. How am I supposed to have a code section that does not belong to any method? How is it possible to invoke it? In general, what is the purpose of this usage? Or better, where can I find documentation about this?

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-10T23:39:32+00:00Added an answer on May 10, 2026 at 11:39 pm

    The code block with the static modifier signifies a class initializer; without the static modifier the code block is an instance initializer.

    Class initializers are executed in the order they are defined (top down, just like simple variable initializers) when the class is loaded (actually, when it’s resolved, but that’s a technicality).

    Instance initializers are executed in the order defined when the class is instantiated, immediately before the constructor code is executed, immediately after the invocation of the super constructor.

    If you remove static from int a, it becomes an instance variable, which you are not able to access from the static initializer block. This will fail to compile with the error ‘non-static variable a cannot be referenced from a static context’.

    If you also remove static from the initializer block, it then becomes an instance initializer and so int a is initialized at construction.

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

Sidebar

Related Questions

This is probably a long shot but I asked a question about converting one
one question about PHP Development. I am building up some Form Fields for Calculations.
i have one question about jquery ajax().. I created a table grid and there
One question about protected constructor. I learnt that the protected constructor can be used
I have one question about searching and counting files in directories and subdirectories. I
I have one question about Yii framework, and I need your help. Basicly, I
now I am working with python. So one question about dict .... suppose I
One more question about most elegant and simple implementation of element combinations in F#.
This is my second question about clicking buttons in JavaScript but this one I'm
I have a question about Criteria method, one-to-many relation to the database, 'one' is

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.