Yin Stadfield wrote:
Ted North wrote:"A static member can call/access only a static member of its own class" (Ganesh & Sharma, 2013, p. 488). Does this mean that a static reference can only call methods in its class or call fields in its class using the name of the class and the dot operator?
Hi Ted,
If I understand your statement correctly, I'll say this is not the case: Does this mean that a static reference can only call methods in its class or call fields in its class using the name of the class and the dot operator?
Because if it is within the class itself, you won't be needing to use the class name + dot operator. Example:
You can't however, call an instance members from a static reference:
Interesting reply. Thank-you for thinking about this some with me. I think in your last example with source-code you meant to assign
instanceInt1 to
staticInt3 to show the compiler error. Also, I think you meant to assign
Example1.instanceInt1 to
staticInt2 to show a compiler error. I see what you meant though. I forgot about how it is a compiler error to assign a instance variable to a static variable.
In my opinion calling a static member without a reference of any kind, class reference or object reference, is outside of the scope of this question. Unless in the statement in question a 'static member' is a method that calls instance members in its own class or a different class after creating instances of the class so that the instance members can be called from a static context.
Wow, this can become confusing.
Thank-you for the help!
Regards,
Ted