function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shilpa SrikantacharShilpa Srikantachar 

Constructor not defined error

Hi All ,
I'm getting the Execute Anonymous Error

Line: 1, Column: 18
Constructor not defined: [Employee_Info].<Constructor>(String, Integer
public class Employee_Info {
    

when the following code is run :


    public String name;
        public Integer exp;
        public Decimal salary;
        public Decimal bonus;
        public Employee_Info(String empName,Integer empExp){
            name=empName;
            exp=empExp;
        }
        public void invoke(){
            if(exp > 5){
                salary=50000;
                bonus=5000;
            }else{
                salary=30000;
                bonus=3000;
            }
        }
        public void show(){
            System.debug('Name :'+name);
            System.debug('Exp :'+exp);
            System.debug('Salary:'+salary);
            System.debug('Bonus:'+bonus);
        }
    }

Pls let me know how to fix it .

Thanks
Shilpa
Best Answer chosen by Shilpa Srikantachar
Puru GangwarPuru Gangwar
It Works...make sure you are passing value in constructer as you have not defiened default constructor.

Employee_Info EI = new Employee_Info('Test', 10);
EI.Invoke();
EI.Show();
As a best practice, local varriable should be defined as Private.

All Answers

Puru GangwarPuru Gangwar
It Works...make sure you are passing value in constructer as you have not defiened default constructor.

Employee_Info EI = new Employee_Info('Test', 10);
EI.Invoke();
EI.Show();
As a best practice, local varriable should be defined as Private.
This was selected as the best answer
Shilpa SrikantacharShilpa Srikantachar
@Puru Gangwar : Thank you it worked but then I have defined the local variable as Private , only Class Variable are Public in the above example . 
Pertaining to which local varaiable you are suggesting as Private .
 
Puru GangwarPuru Gangwar
        I am talking about these varriables. Whenever we design classes we need to define Scope Private unless that varriable is accessed through object. In this scenario these varriable or set by constructor and printed by internal public class. 
        public Integer exp;
        public Decimal salary;
        public Decimal bonus;
Shilpa SrikantacharShilpa Srikantachar
Thank you for the detailed info .