So, I have some code like this:
ArrayList<Class<? extends Piece>> ap = stage.getApplicablePieceList();
for(int i = 0; i < ap.size(); i++){
Class<? extends Piece> currentPiece = ap.get(i);
//Problem here, see rest of post
}Where the comment is, I want to access some static variables of the Piece subclass stored in currentPiece. How would I go about this?
(Notes:
- Piece is a class I coded, the source of it is not currently applicable
- I'm not sure if size() is the right method to get the size of an ArrayList, but I don't have the code on me (I'm at school) so assume it is)
Offline
ZeroLuck wrote:
1. the size() method is right.
2. If you want to access static variables you only have to write:
Piece.myStaticVariable
From my general knowledge of coding, I would think that the above is correct
Offline
What I mean is, currentPiece loops through each subclass of Piece stored in ap. I need to access static variables of the class currently in currentPiece. currentPiece.staticVar doesn't seem to work.
Offline
I have an example for you:
class Main {
public static void main(String[] args) {
// set currentPiece to 0
Piece.currentPiece = 0;
// print the current value
System.out.println("currentPiece is: "+Piece.currentPiece);
// change the currentPiece
Piece.currentPiece += 1;
}
}
class Piece {
public static int currentPiece;
}Offline
Well, I'm an absolute n00b at Java, but isn't the thing *.length instead of size()? Sorry if this refers to something totally different, I couldn't really read the code and I have no idea whatsoever about what an ArrayList is (assuming it's an array).
Offline
maxskywalker wrote:
Well, I'm an absolute n00b at Java, but isn't the thing *.length instead of size()? Sorry if this refers to something totally different, I couldn't really read the code and I have no idea whatsoever about what an ArrayList is (assuming it's an array).
indeed you're right
Offline
Object[] array = new Object[100];
System.out.println(array.length); // use length for arrays.
ArrayList list = new ArrayList();
for(int i = 0;i < 100;i++){
list.add("#"+i);
}
System.out.println(list.size()); // use size() for listsLast edited by ZeroLuck (2011-10-10 08:46:17)
Offline
Try this:
for (Object piece : stage.getApplicablePieceList())
{
System.out.println(((Piece)piece).staticVar);
}Oh, and make sure staticVar is defined in Piece or it won't compile.
Last edited by MathWizz (2011-10-10 21:25:36)
Offline