1

I'm wondering which is a more effective storage method in java. Would be better to save constants in a class or a resource? The constants will be searched later by my Search program which will go through the list of constants so as to recognize possible options. Also if there is a more efficient method of storing them please say so or if i doesn't matter where i store them.

1). A java class called Search is going to be inputted with two or more constants they will then either access a java class that will redirect them as needed.

2). Search will receive the same information but will look to a external resource to see what possible files match it then corresponding what info was give will open a pathway to the java class it correlated.

Which version should be better or will it not matter?

the values the constants contain are going to be ints but each int will have their own unique meaning.

To clarify why i need these arrays each number is going to have a meaning and the arraylists will hold the numbers like 1 may mean work 2 may mean private and so a private work document would have name PDocument has [1,2] so on and so on hope that clarifies

So which option is more effecient #1 or #2

5
  • is this a good question to ask here
    – Bored915
    Commented Mar 20, 2012 at 23:30
  • 4
    It could be, but I feel like you abstracted away too many of the details. I'm having a hard time picturing what the actual goal is. Commented Mar 21, 2012 at 0:17
  • my goal is to create a search method that is going to look for an array that I'm trying to find where to store the variables in with minimal processes required thus the question of whether to use a java class or method or a external resource does that clarify it?
    – Bored915
    Commented Mar 21, 2012 at 0:26
  • @unholysampler did this clarify it or do i need more information and were is it lacking in detail or where is it still unclear
    – Bored915
    Commented Mar 21, 2012 at 2:19
  • this question is a better fit for stackoverflow but I answered it anyway.
    – Mike L.
    Commented Mar 21, 2012 at 3:05

2 Answers 2

1

Would be better to save variables that will not change in a class or a resource?

By definition, a variable is something which will change over time. If it will not change, it is a constant.

Your description sounds, as if there is a index of keywords or topics for documents:

To clarify why i need these arrays each number is going to have a meaning and the arraylists will hold the numbers like 1 may mean work 2 may mean private and so a private work document would have name PDocument has [1,2] so on and so on hope that clarifies

Such keywords or topics may change over time, and then you can't modify them without recompiling the code, if you use enums in your class. Even if the today requirement says, they will not change: In the long run most requirements change. And you might try to solve a different problem, where the list of keywords/topics grows over time.

A map which can be extended, and is read from a file is more flexible.

3
  • Overengineering much? The OP never stated what the scope of this project was. Why introduce file IO and non-strongly typed access to these values? You just added a bunch of unneeded complexity to this project for hardly any gain. Having to recompile every once in a while is not necessarily a bad thing.
    – Mike L.
    Commented Mar 21, 2012 at 15:04
  • @MikeL. Well - how probably he will need new keywords, or reuse the algorithm in a more flexible situation is something only he can answer. I gave a suggestion in combination with the reasons - if the reasons don't fit, forget the suggestion. Commented Mar 21, 2012 at 17:42
  • @MikeL. actually User unkown was spot on i forgot to mention that
    – Bored915
    Commented Mar 21, 2012 at 23:47
1

It sounds like you are talking about variables that will hold constant values. These variables should be declared in the your class that handles the searching.

EX:

private static final int SearchType1 = 0;

You could also create an enum type that will hold your search constants

EX:

public enum SearchTypes {
    SearchType1,
    SearchType2
}

where SearchType1 will be 0, and SearchType2 will be 1

2
  • will one other thing is which would be more efficent meaning which would take less processes
    – Bored915
    Commented Mar 21, 2012 at 3:07
  • Either way will really give the same performance. From a design standpoint I like the enum type better as its easier to read.
    – Mike L.
    Commented Mar 21, 2012 at 3:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.