Skip to main content
Hi,

  I'm trying to execute the below code in developer console anonymous window and I'm getting the error .

Line: 26, Column: 5

Method already defined: <init> void Person.<init>() from the type Person

My Code is :

Public class Person{

    private String firstName;

    private String lastName;

    private Integer age;

    

    Public Person()

    {

        

    }

    

    Public person(String firstName,String lastName,Integer Age)

    {

        this.firstName=firstName;

        this.lastName=lastName;

        this.Age=Age;

    }

    

    Public void printInfo()

    {

        System.debug('First Name :'+firstName);

        System.debug('Last Name :'+lastName);

        System.debug('Age:'+age);

    }

    

    Person P1=new Person(Robert,Chris,30);

    P1.printInfo();

}

Any help would be much appreciated.
4 Antworten
  1. 6. Dez. 2018, 17:45
    Hi Ancy,

    In Apex you have to first save the class like below with out object definition inside the class to excute the class:

    Setup --> Apex Class --> New.

    Public class Person{

    private String firstName;

    private String lastName;

    private Integer age;

    Public person(String firstName,String lastName,Integer Age)

    {

    this.firstName=firstName;

    this.lastName=lastName;

    this.Age=Age;

    }

    Public void printInfo()

    {

    System.debug('First Name :'+firstName);

    System.debug('Last Name :'+lastName);

    System.debug('Age:'+age);

    }

    }

    ========================

    Then only execute the below two lines in Developer Console:

    Person P1=new Person('Robert','Chris',30);

     P1.printInfo();

    Now Open the Log you can see your output.

    Don't include the Object Defnition inside the class  as like java to run the code.

    Can you please Let me know if it helps or not!!!

    If it helps don't forget to mark this as a best answer!!!

    Thanks,

    Maharajan.C
0/9000