You need to sign in to do that
Don't have an account?
Badrinath Murali
code compile error
Hi All,
I am trying to create the below class/method with a constructor to learn Apex:
public class Employee {
public string Name;
public decimal Sal;
public decimal Exp;
public employee(string name,decimal sal,decimal Exp){
Name = name;
Sal = Sal;
Exp = Exp;
public void display(){
sal = sal*exp;
system.debug('Salary='+Sal);
}
}
}
I am getting multiple code complie errors:
unexpected token: 'public'
unexpected syntax: 'missing SEMICOLON at 'display''
Variable does not exist: void
Method does not exist or incorrect signature: void display() from the type Employee
Can somebody please help understand my mistakes in the code.
Many Thanks
Badri
I am trying to create the below class/method with a constructor to learn Apex:
public class Employee {
public string Name;
public decimal Sal;
public decimal Exp;
public employee(string name,decimal sal,decimal Exp){
Name = name;
Sal = Sal;
Exp = Exp;
public void display(){
sal = sal*exp;
system.debug('Salary='+Sal);
}
}
}
I am getting multiple code complie errors:
unexpected token: 'public'
unexpected syntax: 'missing SEMICOLON at 'display''
Variable does not exist: void
Method does not exist or incorrect signature: void display() from the type Employee
Can somebody please help understand my mistakes in the code.
Many Thanks
Badri
In your code the constructor method closing curly bracket was misplaced. It is placed after display() method. Hence the compiler is throwing multiple errors as method definition is not allowed inside a constructor or any other method.
Please try as given below:
public class Employee
{
public string Name;
public decimal Sal;
public decimal Exp;
{
Name = name;
Sal = Sal;
Exp = Exp;
} //This method closing bracket was misplace.
{
sal = sal*exp;
system.debug('Salary='+Sal);
}
}
Please mark it as Solved if it answers your question.
Thanks.
All Answers
In your code the constructor method closing curly bracket was misplaced. It is placed after display() method. Hence the compiler is throwing multiple errors as method definition is not allowed inside a constructor or any other method.
Please try as given below:
public class Employee
{
public string Name;
public decimal Sal;
public decimal Exp;
{
Name = name;
Sal = Sal;
Exp = Exp;
} //This method closing bracket was misplace.
{
sal = sal*exp;
system.debug('Salary='+Sal);
}
}
Please mark it as Solved if it answers your question.
Thanks.
Good Day!!
Badri
Employee display = new Employee();
display.name = 'MyEmployee';
display.sal = 12000;
display.exp = 5;
display.display();
The message is Constructor not defined: [Employee].<Constructor>()
Thanks
Badri
Your constructor is looking for 3 input parameters.
Please try
Employee display = new Employee('MyEmployee', 12000, 5);
display.display();
The output will be logged in log file.
Hope this helps:)
Further questions please put it in a new thread. :)