How does the Intellij choose the constructor when I click Find Usage? -
i have got constructor takes 4 parameters: string, object, object, myenum
.
public myclass(string name, object val1, object val2, myenum cat) { // find in usage = 202 this.name = name; this.val1 = val1; this.val2 = val2; this.cat = cat; }
in 95% usage cases last parameter null
, create constructor takes 3 parameters , sets last 1 null.
some ideas came mind gave try following solution:
change last parameter
myenum
integer
(compilation error - not matter):public myclass(string name, object val1, object val2, integer cat)
- still 202 find usageadd new constructor last parameter of type object:
public myclass(string name, object val1, object val2, object cat)
, first constructor (withinteger
) has 196 usages , second 1 6.
this wanted (i have got 6 invocations non null last element) reason of behaviour? intellij make checks , if input type myenum
cannot passed integer
type, null can passed both, why in case of first constructor there 6 less results?
when changed object
string
(now have 2 constructors string
, integer
) both give me 0 when run find usage.
thank explanation of behaviour.
if have both mymethod(integer a)
, mymethod(object a)
, call mymethod(null)
call integer version, because more specific. similarly, if had mymethod(myenum a)
, mymethod(object a)
, myenum version called if passed null argument. (see this answer null overloads, references section 15.12.2.5 of java language specification.) there's no difference in how it's behaving based on whether it's integer or myenum; both more specific object, that's overload being selected.
when ask intellij idea find usages of method, uses own index of code , knowledge how java handles overload resolution find cases method called. while these rules can complicated (looking @ detail in java language specification makes eyes hurt too), , it's conceivable there's use case idea wrong , see differently way java compiler , runtime would, in case looks getting right. it's handy way play , understand , test how overload resolution works.
Comments
Post a Comment