Question about a Java array's length property

I know that Java's array's length property is defined as public final. My question is, how does the array set a value to that 'length' property if it's final?

For example, if I do:

int arr = new arr[5];

At this point, how does array assign the number 5 to its length property internally if the 'length' property is final?
elepilAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Phil PhillipsDevOps ArchitectCommented:
That is allowed in that case because that's the first (and only) time 'length' is being set ('final' allows for the value to be set exactly once).
0
elepilAuthor Commented:
Thank you for your response, Phil.

Are you saying Java made a "special consideration" that despite being a public final variable, Java allowed array's 'length' to be assigned a value, and that I cannot replicate such a behavior in my own classes?
0
Phil PhillipsDevOps ArchitectCommented:
No, it's just like any final variable.  Final variables are allowed to be set one time.  When you do:

int[] arr = new int[5];

It is essentially creating a new array object, and setting its length value for the first time.  You can do the same thing with your own objects:

public class Test {
   private final int someFinalValue;
   
   public Test(int somefinalValue) {
      // First time the final int is being set - can no longer set it after this
      this.someFinalValue = someFinalValue;
   }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
elepilAuthor Commented:
Ah, thank you for that code sample, that made it clear.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.