user2239404
user2239404

Reputation: 25

The constructor CompanyDetail() is undefined (java)

import java.io.*;
import java.util.*;

public class CompanyDetail {

    int Id;
    String name;
    String department;
    static String companyname="Maruti Suzuki";

    CompanyDetail(int ID,String Name,String Dept) {
        Id=ID;
        name=Name;
        department=Dept;
    }

    public void getdata() {
        try {
            InputStreamReader in = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(in);
            System.out.println("Employee Id :");
            Id = Integer.parseInt(br.readLine());
            System.out.println("Employee name :");
            name= br.readLine();
            System.out.println("Employee Department :");
            department=br.readLine();
            System.out.println("Company is :"+companyname);
        }
        catch(Exception e) {
        }
    }
    public void printdata() {
        System.out.println("Employee Id is :"+Id);
        System.out.println("Employee Name is :"+name);
        System.out.println("Employee Department is :"+department);
        System.out.println("Company is :"+companyname);
    }
}

public class CompanyUse {
    public static void main(String[] args)  {
        CompanyDetail cd = new CompanyDetail(int Id,String name,String department);
        cd.getdata();
        cd.printdata();
    }
}    

i am getting error in a main block when i create a object..eclips keep teeling me that constructor CompanyDetail() is undefined....and without constructor it gives me a null value in my output..please help me ..i ve just started learning java...thank you very much in advance :)

Upvotes: 0

Views: 87

Answers (1)

Marcin Pietraszek
Marcin Pietraszek

Reputation: 3214

Default constructor (with empty parameter list) is created automatically by the compiler only when you don't create any other constructor. In your case you have CompanyDetail(int ID,String Name,String Dept) so it wasn't automatically generated. You could write default constructor yourself:

CompanyDetail() {}

Also I have some tips:

  • this code isn't compiling, please fix all the errors - java compiler is quite good at saying what's wrong with code (:
  • never, ever use empty catch block,
  • try to follow naming conventions popular in Java.

Upvotes: 2

Related Questions