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

  • Home
  • SEARCH
  • 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 6994073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:50:43+00:00 2026-05-27T19:50:43+00:00

I am wondering when static variables are initialized to their default values. Is it

  • 0

I am wondering when static variables are initialized to their default values.
Is it correct that when a class is loaded, static vars are created (allocated),
then static initializers and initializations in declarations are executed?
At what point are the default values are given? This leads to the problem of forward reference.

Also please if you can explain this in reference to the question asked on Why static fields are not initialized in time? and especially the answer given by Kevin Brock on the same site. I can’t understand the 3rd point.

  • 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-27T19:50:44+00:00Added an answer on May 27, 2026 at 7:50 pm

    From See Java Static Variable Methods:

    • It is a variable which belongs to the class and not to object(instance)
    • Static variables are initialized only once , at the start of the execution. These variables will be initialized first, before the initialization of any instance variables
    • A single copy to be shared by all instances of the class
    • A static variable can be accessed directly by the class name and doesn’t need any object.

    Instance and class (static) variables are automatically initialized to standard default values if you fail to purposely initialize them. Although local variables are not automatically initialized, you cannot compile a program that fails to either initialize a local variable or assign a value to that local variable before it is used.

    What the compiler actually does is to internally produce a single class initialization routine that combines all the static variable initializers and all of the static initializer blocks of code, in the order that they appear in the class declaration. This single initialization procedure is run automatically, one time only, when the class is first loaded.

    In case of inner classes, they can not have static fields

    An inner class is a nested class that is not explicitly or implicitly
    declared static.

    …

    Inner classes may not declare static initializers (§8.7) or member interfaces…

    Inner classes may not declare static members, unless they are constant variables…

    See JLS 8.1.3 Inner Classes and Enclosing Instances

    final fields in Java can be initialized separately from their declaration place this is however can not be applicable to static final fields. See the example below.

    final class Demo
    {
        private final int x;
        private static final int z;  //must be initialized here.
    
        static 
        {
            z = 10;  //It can be initialized here.
        }
    
        public Demo(int x)
        {
            this.x=x;  //This is possible.
            //z=15; compiler-error - can not assign a value to a final variable z
        }
    }
    

    This is because there is just one copy of the static variables associated with the type, rather than one associated with each instance of the type as with instance variables and if we try to initialize z of type static final within the constructor, it will attempt to reinitialize the static final type field z because the constructor is run on each instantiation of the class that must not occur to static final fields.

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

Sidebar

Related Questions

I was wondering about when does a static variable(in a class) come into picture(initialized)?
I'm wondering whether static constant variables are thread-safe or not? Example code snippet: void
I'm wondering how static member variables are typically implemented in languages like C++ and
Possible Duplicate: Where are static variables stored (in C/C++)? I am wondering where global
I'm wondering if I can use a static variable for optimization: public function Bar()
I was wondering, why do static Create methods exist? For instance, why use this
I have a static C library (say mylib.a ) and I was wondering if
So I understand what a static method or field is, I am just wondering
I was wondering, what is there any different, on various ways to initialize static
I was wondering, whether the following code are safe. public class GUIBundle { //

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.