简体   繁体   English

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

[英]Static method cannot access instance members of a class

In Liang's 9th edition Introduction to Java Programming it states, "A static method cannot access instance members of a class," (pg 312). 在Liang的第9版Java编程简介中,它指出“静态方法不能访问类的实例成员”(第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: 换一种说法:

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

makes sense, whereas: 有道理,而:

 someNonStaticMethod.myClass

or 要么

 someStaticMethod.myClass

does not. 才不是。 Is the someNonStaticMethod.myClass syntax allowed? 是否允许someNonStaticMethod.myClass语法? 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? 如果不允许,为什么提到静态方法无法访问类的实例成员?

Please help lift my confusion. 请帮助解除我的困惑。

-DI -DI

Accessing an instance member means accessing a field or attribute of 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 x because it is a member field of MyClass that is bound to an instance of MyClass . 因此在方法foo ,您不能引用x因为它是MyClass的成员字段,它绑定到MyClass实例

Also see Understanding Class Members to understand the difference between static members and instance members. 另请参阅了解类成员以了解静态成员和实例成员之间的区别。

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;
    }
}

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. 如果您想了解原因:可以在没有类实例的情况下调用静态方法,因此在调用方法时无论如何都不会存在非静态方法。

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 MyClass is used now test will exist. 测试将仅在使用new MyClass时存在,现在测试将存在。 But if I call static method MyClass.getTest() there is not test instance variable created. 但是,如果我调用静态方法MyClass.getTest() ,则不会创建test实例变量。

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);
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 可以在静态方法中实例化的匿名内部类是否可以访问包含类的实例成员? - Can Anonymous inner class instantiated within a static method has access to instance members of containing class? 单例对象的实例成员或类的静态成员 - Instance members of singleton object or static members of class Spring-仅具有静态成员的类注入实例 - Spring - Inject Instance Of Class with Only Static Members 如何从不同类的主方法访问静态内部类的成员 - how to access members of a static inner class from main method of different class 静态成员与实例成员 - Static members vs instance members 当(在 Java 中)static 方法和具有相同名称的实例方法存在于不同的类中时,我无法访问该方法 - When (in Java) a static method and an instance method with the same name exist in different classes, I cannot access the method 非静态方法如何访问java中的静态成员? - How does non-static method access static members in java? 我们无法从静态方法访问非静态实例,但可以启动类。 怎么样? - We cant access non static instance from a static method, but can initiate a class. how? 无法从静态方法调用实例方法 - Cannot call instance method from static method 无法访问扩展 JPanel 的 class 的公共成员 - cannot access public members of class extending JPanel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM