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
SuAkSuAk 

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.

Thanks,
sfdcMonkey.comsfdcMonkey.com
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
HelloWorld.sayYou();

and

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

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 :)
solve
Thanks
Mark it best answer if it hepls you
 
SuAkSuAk

Hi -

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

HelloWorld hw = new HelloWorld();
hw.sayme();


User-added image




Thanks
sfdcMonkey.comsfdcMonkey.com
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 :)