小编Tom*_*Tom的帖子

通过对象在静态方法上使用非静态变量?爪哇

由于我们不能this在静态方法内部使用,也不能使用非静态变量,为什么我们可以使用在静态方法内部使用非静态变量的对象呢?

这是我的意思:

public int x;
public int y;

public Account(int a, int b) {
    this.x = a;
    this.y = b;
}

public static void Swap(Account acc) {
    int holder;
    holder = acc.x;
    acc.x = acc.y;
    acc.y = holder;
}
Run Code Online (Sandbox Code Playgroud)

所以Swap()将工作,即使对象内的变量不是静态的。我不明白这部分。希望能有所帮助。TIA!

java methods static object non-static

4
推荐指数
1
解决办法
238
查看次数

标签 统计

java ×1

methods ×1

non-static ×1

object ×1

static ×1