Static and non static functions

i wrote a code and i forgot to write static in functiion declaration .so i got an error when i tried to call the function by passing parameters through main but as soon as i added static in declaration the error resolved .why does this happen.i mean if a function is not static it can access all the instance variables and all the functions of class while a static function can’t do so then why does it showed error ?

Hii Kirti,
Since the main is static function so all the variables declared inside the main are static and only static functions can access static variables and that’s why you got the error.

1 Like