Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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). 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

like image 296
Digital Impermanence Avatar asked Dec 02 '14 15:12

Digital Impermanence


People also ask

Does static method has access to an instance member?

Static Methods In short, when you define a static method, you allow access from that method only to static members of the class and you cannot access instance members.

Can instance methods access static members of a class?

Instance method can access static variables and static methods directly. Static methods can access the static variables and static methods directly. Static methods can't access instance methods and instance variables directly. They must use reference to object.

Why static method can access only static members?

In the static method, the method use compile-time or early binding. For this reason, we can access the static method without creating an instance.

Which of the following Cannot be used in static method?

The static method cannot use non-static data member or invoke non-static method directly. The this and super cannot be used in static context. The static method can access only static type data (static type instance variable). There is no need to create an object of the class to invoke the static method.


2 Answers

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.

like image 62
M A Avatar answered Nov 15 '22 17:11

M A


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;
    }
}
like image 22
Atom 12 Avatar answered Nov 15 '22 17:11

Atom 12