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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:26:59+00:00 2026-06-02T16:26:59+00:00

The following code in Java uses a final array of String . final public

  • 0

The following code in Java uses a final array of String.

final public class Main {
  public static final String[] CONSTANT_ARRAY = {"I", "can", "never", "change"};

  public static void main(String[] args) {
    for (int x = 0; x < CONSTANT_ARRAY.length; x++) {
      System.out.print(CONSTANT_ARRAY[x] + " ");
    }
  }
}

It displays the following output on the console.

I can never change

If we try to reassign the declared final array of type String, we cause an error:

final public class Main {
  public static final String[] CONSTANT_ARRAY = {"I", "can", "never", "change"};

  public static void main(String[] args) {
    CONSTANT_ARRAY={"I", "can", "never", "change"}; //Error - can not assign to final variable CONSTANT_ARRAY.
    for (int x = 0; x < CONSTANT_ARRAY.length; x++) {
      System.out.print(CONSTANT_ARRAY[x] + " ");
    }
  }
}

Error: cannot assign to final variable CONSTANT_ARRAY.

However, the following code works:

final public class Main {
  public static final String[] CONSTANT_ARRAY = {"I", "can", "never", "change"};

  public static void main(String[] args) {
    CONSTANT_ARRAY[2] = "always";  //Compiles fine.
    for (int x = 0; x < CONSTANT_ARRAY.length; x++) {
      System.out.print(CONSTANT_ARRAY[x] + " ");
    }
  }
}

It displays

I can always change

This mean that we could manage to modify the value of the final array of type String. Can we modify the entire array in this way without violating the immutable rule of final?

  • 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-02T16:27:01+00:00Added an answer on June 2, 2026 at 4:27 pm

    final in Java affects the variable, it has nothing to do with the object you are assigning to it.

    final String[] myArray = { "hi", "there" };
    myArray = anotherArray; // Error, you can't do that. myArray is final
    myArray[0] = "over";  // perfectly fine, final has nothing to do with it
    

    Edit to add from comments: Note that I said object you are assigning to it. In Java an array is an object. This same thing applies to any other object:

    final List<String> myList = new ArrayList<String>():
    myList = anotherList; // error, you can't do that
    myList.add("Hi there!"); // perfectly fine. 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following snippet uses simple Java code. package pkg; final public class Main {
The following code in Java uses varargs to overload methods. final public class Main
Given the following code : import java.io.*; public class Main { public static void
The following code uses simple arrays of String in Java. package javaarray; final public
I tried using the following code in .java(main activity): final ImageView diskView1 = (ImageView)
I'm confused by the following code: import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class
I wrote the following code: import java.lang.*; import DB.*; private Boolean validateInvoice(String i) {
Given is the following code: import java.io.Serializable; import java.util.concurrent.locks.ReentrantLock; class Data { private int
The following Java code is throwing a compiler error: if ( checkGameTitle(currGame) ) ArrayList<String>
Let's observe the following segment of code in Java that uses System.currentTimeMillis() in a

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.