views:

153

answers:

6

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?

+7  A: 

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;
    }
Roman
+4  A: 

You have defined a method, not a constructor.

Remove the void

Bozho
Bonho, another Roman is another person. I do not answer my question from another account.
Roman
+1  A: 

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;
    }
Tim Drisdelle
A: 

Java constructors don't have return types on their signature - they implicitly return an instance of the class.

Kenned
A: 

(answer deleted)

Thomas
A: 

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.

TofuBeer