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
Syed F RazaSyed F Raza 

Creating List of A Class

Hi All I have the following code
// I am creating a Class called LClass
//This class will have 3 data members
//Two of them primitive and one list of this //class

public class LClass {

public Integer A;
public String B;
public List<LClass> Llist = new List<LClass>();

//So far this code is saving without any issue on
//Developer Console

}

//Then in Execute Anonymous Window I wrote the following code

//I am instantiating the class 
LClass LC = new LClass();
LC.A=100;
LC.B='Myself';

//Now i would like to add this object in the Llist I created in the class.

Llist.add(LC);

//When i execute this code, it gives  me the following error.


Variable does not exist: Llist

Please advise why I am getting this error.
 
Danish HodaDanish Hoda
hi Raza,
First of all, your approach is wrong, you are including the same class as it's child which is a void case.
secondly, 
Llist.add(LC); //this is not a correct syntax
LC.Llist.add(LC); //Use this instead

 
Syed F RazaSyed F Raza
Danish,

thanks for your response.
Can you please explain on the use of child class as I don't have a child class in this code. I wrote the following code in Execute Anonymous Window:
LClass LC = new LClass();
LC.A=100;
LC.B='Myself';

//Now i would like to add this object in the Llist I created in the class.

Llist.add(LC);

 
Danish HodaDanish Hoda

Hi Raza,
Think of this way. 

Llist is a member variable of LClass. 
But in the anonymous window, when you have initialised the class with the object LC, Llist is no-where to be seen.
Thus, use LC.Llist.add(LC) instead.