posted 18 years ago
What you need to understand is that a static member belongs to no instance, but it belongs to the class itself.
On the other hand, instance members are different. There is a copy of them for every instance that you create.
When you are writing code in a static context you must be acquainted that you are not working for an specific instance, but for the class itself, and there is not a notion of an specific instance in that context.
That is why you cannot access instance members, because there is no way to know, within a static context, which specific instance of the class you maybe referring to when you access an instance member.
To illustrate it, see this code:
The static method main cannot access the instance member "name" because it belongs to an specific instance unknown to the static context, in this case under the static context even no instance of the class Jedi exists yet.
However, this restriction does not imply that a static method cannot create instances of objects. For instance:
In this case there is no problem, because the main method is accessing the instance member through the specific instance it belongs to.
The conclusion is, static method cannot access instance members directly, but this does not mean that an static method cannot create new objects. If one of these objects it creates is an instance of its same class, the it could access the instance members of its same class through it.
[ June 06, 2006: Message edited by: Edwin Dalorzo ]