Reputation: 911
I have an array with objects inside. How would I use methods on the array element?
My array is of type Object
. I tried array[i].getSalary()
but this doesn't work.
Upvotes: 0
Views: 6844
Reputation: 15533
First of all, you should almost never use an array of Object
. The reason is that you lose all type information that :
Instead of an array of Object
, use an array of the type corresponding to the types of the object that will be put inside it (or a common base class). Assuming that your class is called Employee
, you should declare your array this way:
Employee[] employeeArray;
(employeeArray
is a better name than array
because it tells what kind of objects it contains, again for readability. In general, prefer explicit names for variables.)
With that solution it is easy to use employeeArray[i].getSalary()
, if the class Employee
contains such method. The intention of this code is also obvious when you read it.
Other possibilities are generic collections like List<Employee>
or Set<Employee>
, depending on your needs.
If you really have to use an array of Object, and call a getSalary()
method, you will have to cast the array elements to the class or interface to which the method getSalary()
belongs.
For example and again, if this class is called Employee
:
Employee employee = (Employee) array[i];
employee.getSalary();
What casting does is obtaining a reference of type Employee of your object. The object is still the same but now you can call methods of Employee
on this object.
But this solution have a number of caveats. First, it is more verbose and it takes two lines to make what could have taken just one. Second, and more importantly, since you have an array of Object
, you can not be certain that you really have an object of type Employee
, and if it is not the case, the operation will throw a ClassCastException
. A solution to this is to first check that the object is really of the desired type:
Object object = array[i];
if (object instanceof Employee) {
Employee employee = (Employee) object;
employee.getSalary();
}
else {
System.err.println("Object is not an Employee: we can not call getSalary()!");
}
But you see that it becomes much more verbose and if you multiply this by the number of times you will have to call a method of these objects, then the code becomes unmanageable.
Upvotes: 4
Reputation: 29233
Do all your objects inside your object[]
have the getSalary()
method?
If not, then you can obviously see why the compiler doesn't let you do that. There's something funny going on somewhere in your code if you end up in this situation.
If yes, is it either the very same getSalary()
method or multiple dynamically bound implementations of the same method in the same class hierarchy?
If neither, you should probably make an interface IHasSalary
(lame name, I know) that exposes such a method, make all these objects implement that interface, then declare the array as IHasSalary[]
.
If yes, find their most specialized common ancestor, say it's the class Employee
, and declare the array as Employee[]
.
Upvotes: 0