+ Start a Discussion
Badrinath MuraliBadrinath 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
Best Answer chosen by Badrinath Murali
Malni Chandrasekaran 2Malni Chandrasekaran 2
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;
public employee(string name,decimal sal,decimal Exp)
{
        Name = name;
        Sal = Sal;
       Exp = Exp;
 }                                //This method closing bracket was misplace.
      public void display()
       {   
       sal = sal*exp;
            system.debug('Salary='+Sal);
        }
    
}

Please mark it as Solved if it answers your question.
Thanks.

All Answers

Malni Chandrasekaran 2Malni Chandrasekaran 2
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;
public employee(string name,decimal sal,decimal Exp)
{
        Name = name;
        Sal = Sal;
       Exp = Exp;
 }                                //This method closing bracket was misplace.
      public void display()
       {   
       sal = sal*exp;
            system.debug('Salary='+Sal);
        }
    
}

Please mark it as Solved if it answers your question.
Thanks.
This was selected as the best answer
Badrinath MuraliBadrinath Murali
Thanks Malni... The code compiled without errors!!

Good Day!!
Badri
 
Badrinath MuraliBadrinath Murali
Malini, Hope you can help me with this one as well, I am trying to invoke this class via annonyamus execution with the below detail:
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
 
Malni Chandrasekaran 2Malni Chandrasekaran 2
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. :)