+ Start a Discussion

static methods should be called with classname

Hi - If static methods should be called with class name and not with class instance, why does the Apex work book (Page 8 ) gives example to call static method with class instance.

Kindly clarify if my understanding of static methods should be called with class name is correct , and why apexworkbook is contradicting.

hi sujatha ArvindKrishnan
i also review apex cookbook page no 8 and eveything is perfact looks there
User-added image

look this here 2 method 1 is static sayYou() and one is non-static method sayMe()
and here is calling method code

User-added image

Ref= https://developer.salesforce.com/docs/atlas.en-us.apex_workbook.meta/apex_workbook/apex2_2.htm

here you see
call static method by


call non-staic method by
HelloWorld hw = new HelloWorld();

so here is clear that when call a static method use only className.staticMethod()
and when call a non-static method first create class instance and than call method by instance :)
Mark it best answer if it hepls you

Hi -

I get this error, when i try to execute..

HelloWorld hw = new HelloWorld();

User-added image

hi ,look into your class in  sayme() method you use static keyword so how can you call a static methd with create class instance so remove static keyword in your sayme() method and save class and call it by create class instance 

as apexwork book class  page no 8 -:
look here there is no static method in HelloWorld class

look here no static keyword use in sayMe() method in apex workbook
if you clear about it Mark it best asnwer if it helps you :)