Hi,
If that is the case, why is it being said that static methods can't access
instance variables?
You are confusing two differents things, it has nothing to do with where
the variables are kept, static methods cannot access instance variables for
different reasons, a call to a static method is done from a class
perspective, Class.StaticMethod() you don't need to have an instance to
call it, therefore you cannot use a variable that is dependand of an
instance to exist.
Static methods will be in the stack right (or is it?). And the instance
pointers will be in the stack too.
No, in the stack you will never find code, the code is kept somewhere else,
the stack is only used to temporary placement of local variables and to keep
a pointer to where return the execution when the method finish the
execution.
So isn't it possible for static methods to 'indirectly' access instance
variables through the instace pointers in the stack?
Why are you assuming that an instance exist?
Maybe an example is good here, take the Math class of the framework, you
NEVER create an instance of it, but you use its static methods.
If you need to have access to instance variables you can do so by passing
it as a parameter:
class A{
private int i;
static int m( A a)
{
a.i = a.i *2;
return a.i;
}
}
As you see you have access to the private members of the instance. Is this
what you are trying to do?
Hope this help,