Question:

Why static method can access only static variables

by  |  earlier

0 LIKES UnLike

Why static method can access only static variables

 Tags:

   Report

2 ANSWERS


  1. Because non-static members are tied to an instance, a particular object, and there is no instance in a static method.


  2. Static methods and variables belong to the class as a whole and not just a single instance like regular class attributes. The static items are shared among any and all instances and are the same ones used for all those instances, so they can only refer to the shared objects and not any instance-specific ones.

    However, it's not precisely true that they can access only static variables - they could also access global variables from outside the class (altho having such exist tends to be poor programming practice.)

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.