0

Consider this code:

// Create a Item oject
item item = new item();

Compiler error message:

Error - Cannot find symbol - Constructor item();

Class item:

public class item
{
    // Attributes
    private String Itemcode;
    private String Itemname;
    private String Description;
    private String Style;
    private String Finish;
    private float  Unitprice;
    private float  Stock;
    private String Suppliercode;
    private String Suppliername;
    private String Address;

    public item(String ItemcodeIn, String ItemnameIn, String DescriptionIn,
                String StyleIn, String FinishIn, float UnitpriceIn, float StockIn,
                String SuppliercodeIn,
                String SuppliernameIn, String AddressIn)
    {
        Itemcode = ItemcodeIn;
        Itemname = ItemnameIn;
        Description = DescriptionIn;
        Style = StyleIn;
        Finish = FinishIn;
        Unitprice = UnitpriceIn;
        Stock = StockIn;
        Suppliercode = SuppliercodeIn;
        Suppliername = SuppliernameIn;
        Address = AddressIn;
    }

And this code is then followed by all of the setter/getter methods for the attributes.

How can I fix this problem?

1
  • 1
    Side Note: In Java, it's customary to use PascalCase for class names and camelCase for variable names. So, if you followed the convention, your class name would be Item and your variable names would be itemCodeIn, itemNameIn, item, etc.
    – Tim Pote
    May 15, 2012 at 18:33

4 Answers 4

10

When you create a parameterized constructor, the default constructor is removed unless you create your own.

Hence you have to explicitly create a default constructor as below:

public Item() {}
0
6

You need to add a default constructor:

public item() {
   // any initialization you need here
}

You should also rename your class to Item (capital I) per best practices, and your fields should begin with a lower case letter.

2

Your Item class only has 1 constructor:

public item(String ItemcodeIn, String ItemnameIn, String DescriptionIn, 
     String StyleIn, String FinishIn, float UnitpriceIn, float StockIn, 
     String SuppliercodeIn, String SuppliernameIn, String AddressIn)

Though you're trying to access it through new Item(); This constructor does not exist since you have over-ridden it with a parametrized one.

You should either provide those arguments when creating your Item or create another, generic constructor:

public Item() {
    // Some code goes here
}
0

Whenever you do not write any constructor , default constructor is by default available within the class. But, if you define write a parametrized constructor within any class, then only that parametrized constructor is available. In such a case, you will have to explicitly define a default constructor.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.