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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:36:20+00:00 2026-05-14T14:36:20+00:00

Where have you mostly used the initialization block? Can you use these to assign

  • 0
  1. Where have you mostly used the initialization block?
  2. Can you use these to assign values to static instance variables?
  3. How is this different from assigning using a constructor?
  4. My book says that the initialization block is executed when the “class is loaded”. What does loading a class mean?

Additional Question
Which is better?

class {static final instance-variable = val} 

or

class {static final instance-variable;  static {instance-variable=val}}
  • 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-05-14T14:36:20+00:00Added an answer on May 14, 2026 at 2:36 pm

    An initialization block is always called regardless of the constructor you’d like to use. So, if the class in question has more than one constructor and you’d like to run some code regardless of the constructor used, then use an initialization block.

    However, when used to assign default values, I’d just assign them directly. If they are even constants, then I’d add final to the list of modifiers as well.

    private static final String VAL1 = "VALUE";
    

    Update since you drastically changed the question, here’s a renewed answer:

    1) Where have you mostly used the initialization block?

    To execute some code regardless of the constructor used.

    2) Can you use these to assign values to static instance variables?

    You’ll need a static initializer block.

    private static final String FOO;
    
    static {
        FOO = "bar";
    }
    

    This has the benefit that you can do more than just assigning a value. E.g. getting it from some method and handling the exception:

    static {
        try {
            FOO = getItSomehow();
        } catch (Exception e) {
            throw new ExceptionInInitializerError(e);
        }
    }
    

    3) How is this different from assigning using a constructor?

    It’s always assigned regardless of the constructor used.

    4) My book says that the initialization block is executed when the “class is loaded”. What does loading a class mean?

    The book is actually talking about a static initialization block. A class is usually loaded when you reference it for the first time in your code. You can also forcibly load it by Class#forName() (such as you do with JDBC drivers). I’ve posted an answer with more examples and explanation before here.

    Hope this helps.

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

Sidebar

Related Questions

I have a 32 bit COM component that is used mostly by ASP, we
We have a set of Mac machines (mostly PPC) that are used for running
I have mostly all of my forms for different controllers in a jquery dialog
i have hundreds of [mostly different] files in many different directories that all have
I have this thing working mostly. What I don't get is, if I have
1) Escape sequences are mostly used for characters constants that either have a special
I have a bunch of controller actions mostly used for saving data to backend
I'm looking for a database system to use with c#. I've mostly used SQL
Mostly i have used $_SESSION , But while reading about Session i got few
I have mostly used generics to make type safe collections.What are the other uses

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.