Java apprentice Java apprentice - 26 days ago 6
Java Question

error: constructor cannot be applied to given types

Here is my code:

public class eggs {

static int pick;

//Constructor
public eggs (int newPick) {
pick = newPick;
}


This part works without problems.

eggs egg = new eggs();


This is the part I'm having problems with, when I compile the class with this in I get the error in the title.

Answer

A constructor must have the same name as the class:

public class Eggs { 
    private int pick;

    //Constructor
    public Eggs (int newPick) {
        pick = newPick;
    }

    public Eggs () {
    }
}

Note that I also capitalized your class to Eggs in accordance with the naming standard.

To address the comments by @Elliott, I made the field non static and private. Also, I defined an empty constructor. Note that Java will only generate a default empty constructor in the event that nothing else be defined, which is not the case for your code.