3

I think I'm running into an inheritance conceptual wall with my Java arrays. I'm kind of new to Java so please tell me if I have things upside down. In essence, I want to do three things:

  1. Create a runnersArray with the attributes of my Runners class.
  2. Fill my runnersArray using my GenerateObjects method of my GenerateObjects class.
  3. Access the contents of my filled runnersArray in my Evaluating method of my Evaluating class.

The problem seems to be that runnersArray is not visible to the methods in steps 2 and 3 above, but their classes (due to design reasons) cannot inherit or extend Runners class.

Thanks in advance for any suggestions.

Here are some code snippets showing what I'm trying to do:

public class Runners extends Party {

    Runners[] runnersArray = new Runners[5];
}

and

public class GenerateObject extends /* certain parent class */ {

     public GenerateObject (int arrayNum) {
          runnersArray[arrayNum] = /* certain Runners attributes */;
     }
}

and

public class Evaluating extends /*certain parent class*/ {

     public Evaluating (int arrayNum) {
          System.out.println(/* String cast attribute of runnersArray[arrayNum]*/;
     }
}
2
  • Based on the skeleton you have posted, why do GenerateObject and Evaluating need implementation details of Runners for what look like trivial operations (assign a member, get a string)? Could not Runners provide methods in place of these other classes mucking with its state directly? Commented Apr 6, 2010 at 3:03
  • I could use Runners for that, but I have about 5 or 6 classes similar to Runners. It would be tedious to code each class in the way you suggest. GenerateObject and Evaluating are designed to be one stop shops for all those classes and do much more complicated operations. Commented Apr 6, 2010 at 3:07

1 Answer 1

2

The way you have it in your code sample, runnersArray is a package-private (because it doesn't have any access specifier, default is package-private) instance variable of the Runners class. So you cannot just access it from within the GenerateObject and Evaluating classes.

If you want runnersArray to be a globally visible object, then perhaps it should be a public static variable.

public class Runners extends Party {
    public static Runners[] runnersArray = new Runners[5];
}

Then you can access it from the other classes like this:

public class GenerateObject extends /* certain parent class */ {
    public GenerateObject (int arrayNum) {
        Runners.runnersArray[arrayNum] = /* certain Runners attributes */;
    }
}

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

This works ideally. Thank you for your practical solution. I will read up on public static in a bid to improve knowledge.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.