So I just spent around an hour trying to unveil and resolve a very strange bug, one that I have never seen before.
I am more or less asking for potential causes of this seeming random cast from enum to String rather than "please fix my code for me".
The gist of the problem is as follows:
I have an interface, call it IFoo
, within this interface, it has a static enum, called Bar
:
public interface IFoo {
static enum Bar{
A,
B,
C,
}
Bar doGetBar();
}
I of course have a Foo
class that implements IFoo
.
public class Foo implements IFoo{
public Bar getBar(){
return Bar.A; // for example
}
}
Somewhere else, I have an object array called
Object[] result;
And a helper method that returns an array of Object
s, holding results from the getBar()
method, call it
public Object[] getBars()
Now, when I do
result = getBars();
result
magically holds String
s instead of Bar
s, the values of the strings are the implementation class of the outer class (not really an outer class, more of a wrapper class?) of Bar
, i.e. "Foo".
Could someone possibly explain to me how this is possible?
Some leads:
1. results were holding strings before the assignment.
2. the static enum inside IFoo is questionable, it was non-static, but it caused ClassCastException
s when I tried to cast it to IFoo.Bar from Foo.Bar (or something along these lines).