File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Array - Security hole Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array - Security hole" Watch "Array - Security hole" New topic

Array - Security hole

Patricia Samuel
Ranch Hand

Joined: Sep 12, 2007
Posts: 300
This is a frequent source of security holes:
// Potential security hole!
public static final Thing[] VALUES = { ... };

These are the lines that i read in effective Java. Please confirm it is because we have made the reference final not the inside things.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42959
The VALUES field is final, meaning that you can't assign a different array to it like "VALUES = ...", but the individual elements *can* be reassigned like "VALUES[0] = ...".
PrasannaKumar Sathiyanantham
Ranch Hand

Joined: Nov 12, 2009
Posts: 110
This is similar to C Pointers.

The variable will always point to the particular address in memory only. But the value stored in the address can be changed

To err is human,
To forgive is not company policy
Rob Spoor

Joined: Oct 27, 2005
Posts: 20188

Two workarounds are:

1) make the array private and create a public static accessor method:

2) use a List:

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Array - Security hole
It's not a secret anymore!