-1
public with sharing class Myfirstvfclass {

    public list<Account> lacc { get; set; }
    public Myfirstvfclass{
        lacc=new list<Account> ([select id from Account]);
    }
}
5

The constructor syntax is incorrect.

We write constructor as a method() with no return type.

public with sharing class Myfirstvfclass {

    public list<Account> lacc { get; set; }
    public Myfirstvfclass() {
        lacc=new list<Account> ([select id from Account]);
    }
}

You can read more about Constructors in apex here: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_constructors.htm

3

The signature for constructor is incorrect, you are missing the parenthesis for constructor.

Below line should be replaced

public Myfirstvfclass // Incorrect without parenthesis

with

public Myfirstvfclass() //correct way of defining the constructor

Your class should be

public with sharing class Myfirstvfclass {

    public list<Account> lacc { get; set; }
    public Myfirstvfclass(){
        lacc=new list<Account> ([select id from Account]);
    }
}
0

your constructor could be done in this way:

public with sharing class Myfirstvfclass {

    public list<Account> lacc { get; set; }
    public Myfirstvfclass() {
        lacc=new list<Account> ([select id from Account]);
    }
}
4
  • should not matter right? intance variable called from instance context? May 23 '19 at 8:14
  • I am wrong, I will correct it
    – m Peixoto
    May 23 '19 at 8:14
  • 1
    You've changed the constructor to a method - a different thing.
    – Keith C
    May 23 '19 at 10:16
  • yup. I could delete, as your answer is better then mine, but I believe it is not recommended to delete
    – m Peixoto
    May 23 '19 at 11:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.