Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to access individual values of an object that is stored in an array of similar objects in Java
    text
    copied!<p>I am having a problem with trying to keep my coding organized and as simple as possible. I basically have an array of similar objects that hold multiple values. I am wanting to access those individual values and be able to modify them at will but cannot seem to acess them. This is what the code basically looks like...</p> <pre><code> //In file Champion.java package Champions; public interface Champion{} //In another file ChoGath.java package Champions; public class ChoGath implements Champion{ public static double health = 440.0; } //Yet another file Ahri.java package Champions; public class Ahri implements Champion{ public static double health = 380.0; } //In the main build file LOLChampBuilder.java package LOLChampBuilder; import Champions.*; public class LOLChampBuilder{ public static Champion[] listOfChampions = {new ChoGath(), new Ahri()}; public static void main(String args[]){ //This next line works System.out.println(new ChoGath().health); //This next line does not work System.out.println(listOfChampions[0].health); } } </code></pre> <p>There are more variables and whatnot but this is the basic problem. </p> <p>ChoGath and Ahri are part of the group Champions and each has their own unique value for health. I want to be able to combine it all into an array for ease of grabbing values because I know where ChoGath (as an example) is in the array. </p> <p>I get the error <code>Cannot find symbol 'health' in Champions.Champion</code>. I have gone and created the value in Champion and that fixes it (and also change it to class and then extends instead of implements) but then when I go to print the value is always 380.0 as it was the most recent edit to the value health in Champion. </p> <p>I want it so that I can group all the "Champions" together under a single array (so they need to be the same object type ie: Champion, <strong>correct me if I'm wrong</strong>) and access their individual values. I cannot seem to do this so I don't know if I need to use ArrayList (which I've never used) or something else entirely. Now I know I could fix this and put it all into a massive file but I am trying to use multiple files for organizational purposes as well as cleanliness. Thoughts on what to do?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload