0

Receiving this error to which most results say Define the constructor, homeboy. Any insights into what error I am making because I thought it is defined in my class. I'm pretty new to java, don't shred me if it's obvious.

Error: constructor KServer in class KServer cannot be applied to given types;  
                KServer server = new KServer(port);
   required: no arguments
   found: int
   reason: actual and formal argument lists differ in length
1 error

KServer.java

public class KServer {
    private int port;

    //isn't this the constructor defined?
    public void KServer(int PORT) {
        port = PORT;
    }
    public void Run() {...}
}

KServ.java

public class KServ {
    public static void main(String[] args) {

    if (args.length != 1) {
        System.err.println("Usage: java KServ <port number>");
        System.exit(1);
    }

    int port = Integer.parseInt(args[0]);
    KServer server = new KServer(port);
    server.Run();

    }

}
4
  • 3
    Remove the void keyword from your "constructor" Commented Feb 26, 2016 at 15:38
  • it had to be a noob mistake. Shit. doesn't it technically return void? Commented Feb 26, 2016 at 15:38
  • 1
    a constructor has no return type - so I guess your "constructor" is interpreted as method Commented Feb 26, 2016 at 15:39
  • nasvel.wordpress.com/2007/12/04/rules-for-java-constructor Commented Feb 26, 2016 at 15:39

2 Answers 2

3

Remove the word void from the constructor definition :

public KServer(int PORT) {
    port = PORT;
}

For more details on how to write constructors you can look here.

Sign up to request clarification or add additional context in comments.

2 Comments

JP won the race, I'll accept his answer, but you still get an upvote for being cool ;)
@Chemistpp Lol. Yeah.
2

No return type for a constructor, otherwise you define a method.

public KServer(int PORT) {
    port = PORT;
}

1 Comment

You won by a few seconds. Damn ! Upvote for your victory.

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.