为什么我们不能用instance variable一个static method?我知道静态方法是在不创建类实例的情况下调用的,但是什么限制了静态方法中使用的非静态变量?
class MyClass
{
// non-static instance member variable
private int a;
//static member variable
private static int b;
//static method
public static void DoSomething()
{
//this will result in compilation error as “a” has no memory
a = a + 1;
//this works fine since “b” is static
b = b + 1;
}
}
Run Code Online (Sandbox Code Playgroud)