Java 静态方法无法访问类的实例成员

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27252915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:03:26  来源:igfitidea点击:

Static method cannot access instance members of a class

javaclassstatic-methods

提问by Digital Impermanence

In Liang's 9th edition Introduction to Java Programmingit states, "A static method cannot access instance members of a class," (pg 312). I see why an instance member of a class would need to access a method (which might be static), but why would a method need to access an instance member? To me, "access" means "access by way of the dot operator." In other words:

在 Liang 的第 9 版Java 编程简介中指出,“静态方法不能访问类的实例成员”(第 312 页)。我明白为什么类的实例成员需要访问方法(可能是静态的),但为什么方法需要访问实例成员?对我来说,“访问”的意思是“通过点运算符访问”。换句话说:

 Class myClass = new Class();
 myClass.someStaticMethod();

makes sense, whereas:

有道理,而:

 someNonStaticMethod.myClass

or

或者

 someStaticMethod.myClass

does not. Is the someNonStaticMethod.myClass syntax allowed? I don't believe I've ever seen such formatting. If it is not allowed, why mention that static methods cannot access instance members of a class?

才不是。是否允许使用 someNonStaticMethod.myClass 语法?我不相信我见过这样的格式。如果不允许,为什么提到静态方法不能访问类的实例成员?

Please help lift my confusion.

请帮助解除我的困惑。

-DI

-DI

采纳答案by M Anouti

Accessing an instance member means accessing a field or attributeof the instance, not the instance itself since that would not compile. A dot does not literally mean "accessing" in the exact way you think and I guess that's the source of confusion you have. The dot is used to call a method on a certain object (see this link) or to access a field of an object (or class if the field is static).

访问实例成员意味着访问实例的字段或属性,而不是实例本身,因为它不会编译。点的字面意思并不是按照您所想的确切方式“访问”,我想这就是您感到困惑的根源。点用于调用某个对象上的方法(请参阅此链接)或访问对象的字段(或类,如果该字段是静态的)。

For example, assuming the class is defined as follows:

例如,假设类定义如下:

class MyClass {

   private int x;

   static void foo() {
      ... // foo cannot access member x
   }
}

So in method foo, you can't reference xbecause it is a member field of MyClassthat is bound to an instanceof MyClass.

因此,在 method 中foo,您不能引用,x因为它MyClass是绑定到 的实例的成员字段MyClass

Also see Understanding Class Membersto understand the difference between static members and instance members.

另请参阅了解类成员以了解静态成员和实例成员之间的区别。

回答by javaHunter

A static method cannot refer to a non-Static instance field of a class.

静态方法不能引用类的非静态实例字段。

If you want to understand why: A static method can be called without having an instance of a class, thus a non-static would not exist anyway when the method is invoked.

如果您想了解原因:可以在没有类实例的情况下调用静态方法,因此在调用该方法时无论如何都不存在非静态方法。

回答by brso05

It is talking about this:

它在谈论这个:

public MyClass
{
   private String test;

   public static String getTest()
   {
       return this.test; //this is not possible because a static method cannot access an instance variable or method
   }

}

The reason a static method can't access instance variable is because static references the class not a specific instance of the class so there is no instance variable to access. Test will only exist when new MyClassis used now test will exist. But if I call static method MyClass.getTest()there is not testinstance variable created.

静态方法不能访问实例变量的原因是因为静态引用了类而不是类的特定实例,所以没有实例变量可以访问。测试仅在new MyClass使用时存在,现在测试将存在。但是如果我调用静态方法MyClass.getTest(),则不会test创建实例变量。

回答by Francesco Menzani

You cannot access instance variables from static methods.

您不能从静态方法访问实例变量。

public class Example {
    private Object instanceVariable;
    public static void staticMethod() {
        // Cannot use this in a static context
        this.instanceVariable = null;
    }
}

You can access instance variables from instance methods.

您可以从实例方法访问实例变量。

public class Example {
    private Object instanceVariable;
    public void instanceMethod() {
        this.instanceVariable = null;
    }
}

You should not access static variables from instance methods using this.

您不应该使用this.

public class Example {
    private static Object staticVariable;
    public void instanceMethod() {
        // The static field Example.staticVariable should be accessed in a static way
        this.staticVariable = null;
    }
}

You can always access static variables. You should use the class name.

您始终可以访问静态变量。您应该使用类名。

public class Example {
    private static Object staticVariable;
    public void instanceMethod() {
        Example.staticVariable = null;
    }
}

回答by Muli vinod

it is possible to access instance variables in static methods by creating objects

可以通过创建对象来访问静态方法中的实例变量

public class Test {
int x =10;

public static void main(String[]args)
{
    Test t1 = new Test();
    System.out.println(t1.x);
}

}

}