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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:09:48+00:00 2026-05-26T21:09:48+00:00

A final field has to be initialized only once in Java by convention. No

  • 0

A final field has to be initialized only once in Java by convention. No other initializations are permitted. The following program demonstrates the use of final and final static fields within a class.

package main;

import java.util.ArrayList;
import java.util.List;

final public class Main
{
    private final static int a=10;
    private final List<String> list;

    public Main()
    {
        list=new ArrayList<String>();
        list.add("A");
        list.add("B");
        list.add("C");
        list.add("D");
    }

    private void show()
    {
        for(String l:list)
        {
            System.out.println(l);
        }
    }

    public static void main(String[] args)
    {
        new Main().show();
    }
}

In the above code, the final field list of type ArrayList is being initialized within the constructor itself. The same thing is however not applicable to the final static field a. An attempt to initialize a within the constructor body causes a compile-time error indicating that “can not assign a value to a final variable a”. Why is the same thing not applicable to the final static field list which is being allowed to initialize within the constructor?

  • 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-26T21:09:48+00:00Added an answer on May 26, 2026 at 9:09 pm

    The same thing is applicable to final static fields – but in the closest static equivalent to a constructor, namely a static initializer:

    private final static int x;
    
    static {
        x = 10;
    }
    

    (Arguably that’s closer to an instance initializer than to a constructor, but it’s the closest you get for static. It’s not like you can specify arguments or anything.)

    The constructor is run on each instantiation of the class – if that were able to change a final static field, it wouldn’t be very final, would it? Remember that there’s just one “copy” of the static variable associated with the type, rather than one associated with each instance of the type as with instance variables.

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

Sidebar

Related Questions

The following program has no importance of its own. It just counts the number
If I do the following. Create a HashMap (in a final field) Populate HashMap
I am writing a program that has two final variables that I wish to
What happens when a superclass has a field marked final, but a subclass overrides
Is it true that the assigned final object field may still be null inside
If I have a boolean field like: private static final boolean DEBUG = false;
The Java Memory Model (since 1.5) treats final fields differently to non- final fields.
My final goal is to write the program which can run on the Unix
Observe the following definition of a thread subclass (the entire runnable Java source file
Does checkstyle has rule which checks if subclass overrides public or protected field of

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.