File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with privatizing array

 
Adam Price
Ranch Hand
Posts: 95
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "enter it arg by arg" ?
 
Adam Price
Ranch Hand
Posts: 95
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 95
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Adam Price:


not like this:


Why?
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
He said
and for my obscure reasons
 
Adam Price
Ranch Hand
Posts: 95
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 95
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic