Static method cannot access instance members of a

2020-03-02 03:07发布

问题:

In Liang's 9th edition Introduction to Java Programming it 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:

 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?

Please help lift my confusion.

-DI

回答1:

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.

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



回答2:

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.

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


回答3:

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.



回答4:

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. But if I call static method MyClass.getTest() there is not test instance variable created.



回答5:

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

}