Suppose we create an listOfObjects of type ArrayList, which contains objects of type Object:
public static ArrayList listOfObjects = new ArrayList();
We have an Object constructor, which creates an Object with two fields, name and type:
public Object(String name, int type) { /*some code*/ }
Now we create two Objects, then add them to listOfObjects:
public static Object object1 = new Object("object one", 1);
public static Object object2 = new Object("object two", 2);
listOfObjects.add(object1);
listOfObjects.add(object2);
Assuming that object1 and object2 were correctly added to listOfObjects, how can we access the type field of an object in the list?
I have tried
listOfObjects.get(1).type;
but that doesn't seem to work. Is there a way to do this?
EDIT Object is an example name.
nameandtypefields declared in yourObjectclass (which is a bad bad bad name for it)? If not the should be.default non parameterized constructor, because it should be defined if you have another one, take a look at my answer.