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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:09:18+00:00 2026-06-03T07:09:18+00:00

Why aren’t static final variables given default values, whereas static (but non-final variables are

  • 0

Why aren’t static final variables given default values, whereas static (but non-final variables are given default values).

What is the reason that such behavior was implemented in Java?

  • 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-03T07:09:19+00:00Added an answer on June 3, 2026 at 7:09 am

    Of course static final variables are given default values, see for example this:

    class Test {
        static final int x;
        static {
            printX();
            x = 42;
            printX();
        }
    
        static void printX() {
            System.out.println("Here x is "+x);
        }
        public static void main(String[] args) { }
    }
    

    The output is:

    Here x is 0
    Here x is 42
    

    If x wasn’t given the default value of 0 as specified in JLS 4.12.5, the output would depend on the JVM used. You might see some random number.

    Update: Now that we have demonstrated that static final fields do get a default value, you may want to know why the default value is not enough. There is no good answer to that question, besides the obvious one: “The spec says so”. Excerpt from 8.3.1.2:

    It is a compile-time error if a blank final (§4.12.4) class variable is not definitely assigned (§16.8) by a static initializer (§8.7) of the class in which it is declared.

    We can only guess at the motivation behind such a restriction, but I think it’s to make programs easier to understand. If you want to set the variable to 0 it’s clearer to do it explicitly.

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

Sidebar

Related Questions

Why aren't final variables default initialized? Shouldn't the default constructor initialize them to default
I want to replace all symbols that aren't letters by - , but my
Possible Duplicate: Why aren't static const floats allowed? Is there any reason why this
My variables aren't producing data like they should be. For whatever reason, my string
The numbers themselves aren't relevant. I have a list of variables that are used
Why aren't copy constructors chained (like default ctors or dtors) so that before the
Aren't Javascript blocks supposed to execute in the order that they are placed on
So multi-select boxes aren't the nicest inputs to work with in the word, but
It now makes sense that the versions aren't right. I know I am using
What are some key bindings that aren't included?

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.