I'm not sure what you mean by 'iterate through elements of a class', but you can declare static member variables in Java that will be shared by all instances.

So you can declare a static counter...