+ Start a Discussion
prasanth puvvada 4prasanth puvvada 4 

this keyword error, please help

i have a simple apex program and i am trying to execute it from "developer console" i am getting this error.  " unexpected token: thisexample  at line 11"                  please help... thanks in advance

public class thisexample {
public integer age=10;
    public thisexample(integer age)
    {

        this.age=age;

System.debug('the value of age is '+age);    
}
}
thisexample t1=new thisexample(22);
t1.age;
Best Answer chosen by prasanth puvvada 4
surasura
from your screen shot what I can get is you are not running this on execute annonymous. becuase  developer consloe you window has name thiskey.apxc. above code work find in execute annonymous .

if this is a  Class you cant have code after closing parethersis  so remove line 11 and 12

All Answers

surasura
try this 
 
public class thisexample {
public integer age=10;
    public thisexample(integer age)
    {

        this.age=age;

System.debug('the value of age is '+age);    
}
}
thisexample t1=new thisexample(22);
system.debug('Age:'+t1.age);

 
prasanth puvvada 4prasanth puvvada 4
thankyou very much for your answer but still error coming.  please see this screen shot.  thanks in advance.

User-added image
surasura
from your screen shot what I can get is you are not running this on execute annonymous. becuase  developer consloe you window has name thiskey.apxc. above code work find in execute annonymous .

if this is a  Class you cant have code after closing parethersis  so remove line 11 and 12
This was selected as the best answer
prasanth puvvada 4prasanth puvvada 4
sir thank you very much. it is working now..   Sir please tell me what is the difference between annonymous execution and class execution.  
surasura
execute anonymous is used to execute your logic (eg: by calling methods in classes) while classes are used to define the logic . if issue is solved please mark this as sloved by marking the best answere