Java constructors cannot use varargs

I have the following enumeration, which contains many constructors:

public enum Route
{
   HOMEPAGE("",null,UserType.GUEST);

   Route(String baseName,String langFile,Entity entity) {}
   Route(String langFile,Entity entity)  {}
   Route(String langFile,UserType... availability) {}
   Route(String baseName,UserType... availability) {}
}

In this case, when I define homepage, I clearly call the fourth constructor But the problem is that I received an error: the constructor route (java.lang.string, com. Foo. UserType) cannot be parsed

If I remove varags from the constructor, it looks like:

Route(String baseName,UserType availability) {}

Or, if I change null when defining homepage, that is:

HOMEPAGE("","",UserType.GUEST);

Then it works But it doesn't make sense to me Why not detect that I'm calling the fourth constructor?

Solution

The problem is that null can be string or UserType So:

HOMEPAGE("",UserType.GUEST);

Will match the third or fourth constructor Converting null to string will result in the selection of the fourth constructor:

HOMEPAGE("",(String) null,UserType.GUEST);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>