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?

3
  • 2
    void is to be used for methods, not for constructors. Commented Mar 16, 2010 at 14:34
  • @Roman did you just answer your own question with a different account? Commented Mar 16, 2010 at 18:54
  • @Bozho, No. Another Roman is another person. Commented Mar 17, 2010 at 9:21

5 Answers 5

9

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;
    }
Sign up to request clarification or add additional context in comments.

Comments

5

You have defined a method, not a constructor.

Remove the void

1 Comment

Bonho, another Roman is another person. I do not answer my question from another account.
2

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;
    }

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.