Hi there,

This is just going to be a quick question. Also just note that I am i some "remote location" so I cant even show you my code.
But what I want to know is this: Say you have a package called "myPackage" and inside that you have 2 classes, firstClass() & secondClass(). In firstClass() there is a List. At runtime I want to use the getSelectedIndex() method of the List from secondClass().

So if firstClass() & secondClass() have public access, you do this right?:

int index = myPackage.firstClass.myList.getSelectedIndex();

Hope I have the right idea here.. But in my app that I'm working on currently (I'm using Netbeans for this) I keep on getting errors stating that it cannot find "myPackage" or "firstClass" or whatever. What can be the problem? If both classes have public access how can an error like that appear?

Thanks for your help!
Sorry if the questions if a bit vague/confusing...

Recommended Answers

All 6 Replies

is myList static?

No I don't think it is, sorry, I know this doesn't really help you to help me... If it was, would that probably be the reason it can't find the List and all that? Otherwise it should be working right?

You refer to the list using the class name, so that will only work if the variable is static. If it's not static you need to refer to it through an instance of the class, as in

myPackage.firstClass otherClassInstance = ... get a reference to an instance of the other class
int index = otherClassInstance .myList.getSelectedIndex();

Once this is fixed you can see if there are any other problems ;-)

Ah!! Yes I think that is where I went wrong. Well done, thank you so much for your help :-)

Just a quick one: It would then clearly just be easier to declare the List as static than refer to it through a class instance and all that right?

Just a quick one: It would then clearly just be easier to declare the List as static than refer to it through a class instance and all that right?

Wrong way to think about it!
Either there is one list for the whole class, or each instance needs its own list. That tells you whether it's static or not.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.