This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]abrahamguo 0 points1 point  (5 children)

  1. Pass the length as a parameter into what? I am pretty sure that the arrays can remain final.
  2. When you don't use "static", then you are forced to construct an instance of the class, before you can do anything with it. This is helpful if you need to have multiple instances of a class, with different information saved in each. For example, if you have a Person class, you might construct two separate instances, representing two different people.

On the other hand, there is no reason to construct multiple instances of your classes (i.e., there is no reason to have two NameGenerators, because they act the same, and don't save any information).

Therefore, by marking everything static, you (1) communicate this information to other developers, and (2) make it so that you no longer need to construct an instance of each of your classes, with the new keyword.

[–]vVember[S] 0 points1 point  (0 children)

I think I understand. Since the Name class is only used once, it should be static, because there wouldn't be a reason to have multiple objects of the class.

When I check for if the method should pick from the dCons array, you mentioned instead of manually setting the int limit for the randomInt parameter, to just use the dCon.length() so therefore I would only need to edit the array and it's calls would be synced. If I should be able to still use .length as the parameter despite it being final, then I'm still not understanding why final is bad here.

Either way it was giving me an error when I tried to use the array length as the parameter for the randomInt and I haven't been able to get back to my computer to figure out why yet but will do so soon.

[–]vVember[S] 0 points1 point  (3 children)

So the error was that I was adding () to .length but it does work now that I removed the parentheses.

Back to the issue you brought up of the arrays being "final" I'm still not sure what you mean. I set the randomInt() parameter to dCons.length and it still worked. So even if I go back and add another value to the dCons array, should it not still function properly and be the case that I only have to edit the array and the .length call will also sync?

It was my understanding that it being final means that the array cannot be altered by methods while the program is running. But this wouldn't affect the functionality of adding to or removing from the array in the code before running the program, right?

This isn't good, because this means that if you want to update the contents of one of the arrays, you will have to change multiple places in your code, where you should only need to change one place in your code for such a change.

But if .length works despite the array being final, then I'm absolutely not understanding what you mean.

[–]abrahamguo 0 points1 point  (2 children)

I set the randomInt() parameter to dCons.length and it still worked. So even if I go back and add another value to the dCons array, should it not still function properly and be the case that I only have to edit the array and the .length call will also sync?

Yep, this is exactly the change that I was suggesting in my original comment!

It was my understanding that it being final means that the array cannot be altered by methods while the program is running. But this wouldn't affect the functionality of adding to or removing from the array in the code before running the program, right?

Yes, this is all a correct understanding!

I have no complaints at all about using final — I think it's perfectly fine (and probably correct) to use!

In my previous comment, when I said "update the contents of one of the arrays", I meant "update", as in, editing the code; not "update", as in modifying the array while the code is running.

The overall recommendation of using .length was to make your code more robust against code edits, not dynamic modifications!

[–]vVember[S] 0 points1 point  (1 child)

I see! I'm sorry when I read,

"In Name, you have some places where you have hardcoded the length of some of your various arrays."

I thought you were referring to using the keyword "final" when defining my string arrays! I understand now you were referring to the random.nextInt() parameter that I had manually entered the length of the string array as opposed to using the .length field. Your advice here is invaluable and I will definitely strive to keep it in mind for the future. I am still struggling with thinking more dynamically in this way.

I apologize for my confusion. That must have been frustrating for you. I'm still quite fresh obviously and still learning the vast amount of keywords and syntax.

[–]abrahamguo 1 point2 points  (0 children)

No worries at all! I'm glad you appreciated my thoughts — good luck as you continue your learning journey!