aspose file tools*
The moose likes Beginning Java and the fly likes Help with privatizing array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with privatizing array" Watch "Help with privatizing array" New topic
Author

Help with privatizing array

Adam Price
Ranch Hand

Joined: Nov 11, 2005
Posts: 95
I have an class that consists of a main method, one other method and an array. It does exactly what I want if I structure it like so:

I have been told by my betters, however, that I the array "is not data you are going to change, so you should move it outside of the method and make it final."
How do I do that. I have tried a half dozen combinations and none work. I should say that I know how I would do it if I was declaring the array like this: My array is actually much longer than the one I have given and for my obscure reasons, I would like to enter it arg by arg. Is it still possible to "move it outside of the method and make it final."?

Thanks,

Adam
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

What do you mean by "enter it arg by arg" ?


[My Blog]
All roads lead to JavaRanch
Adam Price
Ranch Hand

Joined: Nov 11, 2005
Posts: 95
Originally posted by Satou kurinosuke:
What do you mean by "enter it arg by arg" ?


I want to enter my array like this:


not like this:
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

If you declare the array variable as final, it's the reference to the object (the array) that's final. But the array is still mutable, so you can change the elements...

[ January 12, 2006: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

You could declare it like private static final String[] name;
(Note that you can still change the content of the array anywhere)
and initialize it in the static clause :



That would put the initialization outside, but I'm not sure that's what you meant.
Curtis Brown
Greenhorn

Joined: Jan 10, 2006
Posts: 20
Maybe I'm missing what you're really asking (I'm very new to Java) but I'm stuck on your comment about adding items to the array arg by arg...

I'm envisioning a special method to append the arg to the array, regardless of the size of the array... methinks it'd be something like:



I know this won't compile, but maybe it get's the idea through...
Adam Price
Ranch Hand

Joined: Nov 11, 2005
Posts: 95
Originally posted by Satou kurinosuke:

That would put the initialization outside, but I'm not sure that's what you meant.

I'm not sure either, but it compiles - I'll give it a shot.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9052
    
  12
Originally posted by Adam Price:


not like this:


Why?


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

He said
and for my obscure reasons
Adam Price
Ranch Hand

Joined: Nov 11, 2005
Posts: 95
Originally posted by Marilyn de Queiroz:


not like this:<hr></blockquote>

Why?


Because the "names" are not actually names, they are the names of numerals and there are numerical relationships between the index of each entry and the number name stored therein. There are 30 of them, not all of which have the same relationship. Additionally, not all of the registers are used (is that what I mean? There is no name[ 22 ] or name[ 23 ]. Having it read:
makes it easier for me to make sure that the relationships are being maintained as I tweak the code..
Adam Price
Ranch Hand

Joined: Nov 11, 2005
Posts: 95
Originally posted by marc weber:
If you declare the array variable as final, it's the reference to the object (the array) that's final. But the array is still mutable, so you can change the elements...


If I understand you correctly, I could change the specific values stored in myArray, but not change the size of the array or the type of data it holds, or change myArray to a boolean value. Yes?

How could I "finalize" the values themselves?
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9052
    
  12
Originally posted by Adam Price:
If I understand you correctly, I could change the specific values stored in myArray, but not change the size of the array or the type of data it holds, or change myArray to a boolean value. Yes?

How could I "finalize" the values themselves?

True.

To finalize each of the values, you would need to declare each value using the keyword "final".
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Hunh?

Adam, if your goal is to prevent the elements of the array from being changed after assignment, let's be clear. That is absolutely no way to make the elements of an array final. The best you could do is make sure that within this class you don't change its values inappropriately. (Making it private will ensure it's not changed (because it's not accessible) from outside classes).

[Adam]: ...for my obscure reasons, I would like to enter it arg by arg.

This and other comments make me think maybe you should consider using an ArrayList instead. Have you learned about those yet? Possibly you might want to do something like this:

[ January 13, 2006: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with privatizing array