Reputation: 131268
Well, maybe it is a stupid question, but I cannot resolve this problem.
In my ServiceBrowser
class I have this line:
ServiceResolver serviceResolver = new ServiceResolver(ifIndex, serviceName, regType, domain);
And compiler complains about it. It says:
cannot find symbol
symbol : constructor ServiceResolver(int,java.lang.String,java.lang.String,java.lang.String)
This is strange, because I do have a constructor in the ServiceResolver:
public void ServiceResolver(int ifIndex, String serviceName, String regType, String domain) {
this.ifIndex = ifIndex;
this.serviceName = serviceName;
this.regType = regType;
this.domain = domain;
}
ADDED:
I removed void
from the constructor and it works! Why?
Upvotes: 6
Views: 3355
Reputation: 61546
Welcome to the mistake everyone makes once. As Roman points out you have to delete "void" from infront of the the constructor.
Constructors declare no return type - which may seem odd since you do things like x = new X(); but you can consider it like this:
// what you write...
public class X
{
public X(int a)
{
}
}
x = new X(7);
// what the compiler does - well sort of... good enough for our purposes.
public class X
{
// special name that the compiler creates for the constructor
public void <init>(int a)
{
}
}
// this next line just allocates the memory
x = new X();
// this line is the constructor
x.<init>(7);
A good set of tools to run to find this sort of mistake (and many others) are:
That way when you make other common mistakes (you will, we all do :-), you won't spin your wheels so much looking for the solution.
Upvotes: 0
Reputation: 578
Java constructors don't have return types on their signature - they implicitly return an instance of the class.
Upvotes: 0
Reputation: 2477
That's no constructor... it's a simple method that returns nothing. Absolutely nothing!
Should be this:
public ServiceResolver(int ifIndex, String serviceName, String regType, String domain) {
this.ifIndex = ifIndex;
this.serviceName = serviceName;
this.regType = regType;
this.domain = domain;
}
Upvotes: 2
Reputation: 66216
delete void from signature
public ServiceResolver(int ifIndex, String serviceName, String regType, String domain) {
this.ifIndex = ifIndex;
this.serviceName = serviceName;
this.regType = regType;
this.domain = domain;
}
Upvotes: 9