my dog learned polymorphism*
The moose likes Beginning Java and the fly likes static final array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static final array" Watch "static final array" New topic

static final array

Rahul Bajaj
Ranch Hand

Joined: Sep 21, 2004
Posts: 45
Can arrays be static and final?
Mahesh Bhatt
Ranch Hand

Joined: Sep 15, 2004
Posts: 88
Hi Miki,

I see no problems there. You can definately declare an array as final and static both.

If you are having a problem somewhere in a program, I guess it might be because you are either declaring the array inside a static context, or may be you are trying to change the reference of this array .

I am writing a code here :

This will give you a compilation error because you are trying to change the reference value of the array that has been declared as "final". This is illegal.

however, in the following code :

there is no problem here. This code compiles and prints the output :

so, the moral of the story is : "when you declare an array as final, you can change the elements in the array, however you cannot change the reference of this array."

Hope it helps you

All the best

Impossible is I M Possible
Rahul Bajaj
Ranch Hand

Joined: Sep 21, 2004
Posts: 45
Thank You for your reply.It cleared my confusion.
It is sorta covered in the JavaRanch Style Guide.
subject: static final array
Similar Threads
Stateless Session Bean variables static or non-static.
alternative for interface
can main be declaired final ? why
Question about class and interface
final static int i =0 Whizlabs says i is variable and i feel it is a constant