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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:38:25+00:00 2026-06-02T08:38:25+00:00

I know that once a final variable has a value assigned to it, it

  • 0

I know that once a final variable has a value assigned to it, it cannot be changed. However I just have a couple of questions regarding this:

  • When I have a field, say static final JButton button; outside a class, and then in the main method, try to assign it a value, button = new JButton("OK");, I get an error telling me to remove the final modifier? However since the original button variable does not yet reference an object I was under the impression I could assign it once?

  • Secondly, if I completely remove reference to the button so I just have static final JButton button; outside the class, my IDE claims “The blank final field button may not have been initialised.” Does this mean that all final fields must be initialised? And if so, must they be initialised there and then as I can’t seem to initialise it later.

  • Also, silly question, but my initial assumption that when a final variable is referenced to an instance or data type it can not be assigned to anything else is correct, right?

This code is not complete but is provided to illustrate my point:

public class FinalVarTester {

    static final JButton button;

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        Container container = frame.getContentPane();
        container.setLayout(new BorderLayout());
        button = new JButton("OK");
        container.add(button, BorderLayout.SOUTH);
    }

}
  • 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-02T08:38:28+00:00Added an answer on June 2, 2026 at 8:38 am

    You should initialize a static final variable either in a static initializer, or directly. So either

    static final JButton button = new JButton();
    

    or

    static final JButton button;
    
    static {
      button = new JButton();
    }
    

    The Java language specification has some more documentation about it: the section about final variables specifies why you get the compile error:

    It is a compile-time error if a final variable is assigned to unless it is definitely unassigned (§16) immediately prior to the assignment.

    and chapter 16 talks about the definite assignment

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

Sidebar

Related Questions

I know that similar questions have been asked all over the place, but I'm
I know the location of the DerivedData folder that Xcode creates once I build
I know that exceptions have a performance penalty, and that it's generally more efficient
I know that this has already been asked here but the answer (using a
This is a pretty newbie question- please know I'm aware of that. I have
Ok so I have a class that extends SurfaceView and overrides surfaceChanged - just
Assume that the array has integers between 1 and 1,000,000. I know some popular
I know there have been similar/related questions on SO and elsewhere, and I at
I know that it's a subject that can raise a lot of debate, but
I know that one of the differences between classes and structs is that struct

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.