-3

I have one static class in that where I'm calling one methods using this pointer in my apex class, I'm getting below error.

This cannot be referenced in a static context

below statement shows you how I'm calling in the class

 ApexPages.Message myMsg1 = this.methodToValidate(true);

How can I call this method in place of this pointer???

  • You have to use the name of the class instead of this. So it looks like MyClass.methodToValidate(true); – rael_kid Sep 24 '18 at 5:42
  • When I'm using with the name of the class I'm getting error like below Non static method cannot be referenced from a static context: ApexPages.Message AuraSampleController.methodToValidate(Boolean) – user59569 Sep 24 '18 at 5:43
2

The this keyword is used to identify the current instance of a class. Static functions do not run in an instance context so this has no real meaning. If you want to call a static method from another static method in the same class, you use the name of the class instead of this.

Based on your comments, it seems that you're trying to call a non-static method from a static context. If you would like to call the instance method methodToValidate from a static function, you first have to make an instance of the class. So in your static method you use something like this:

AuraSampleController sampleController = new AuraSampleController();
sampleController.methodToValidate(true);

It seems though that you're lacking basic understanding of the difference between static and instance functions. I suggest you read up on this, the Apex documentation regarding static and instance methods could be a good place to start. The static keyword in Apex has a lot in common with for instance Java's static, but there are differences, so keep that in mind when using documentation for a different language.

| improve this answer | |

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