Static method cannot access members of class instance
In the 9th edition, Liang's Introduction to Java Programming states, "A static method cannot access the members of an instance of a class" (p. 312). I can see why a member of an instance of a class would need to access a method (which could be static), but why would a method need to access an instance member? For me, "access" means "access via the dot operator". In other words:
Class myClass = new Class();
myClass.someStaticMethod();
makes sense, whereas:
someNonStaticMethod.myClass
or
someStaticMethod.myClass
not. Is the syntax someNonStaticMethod.myClass allowed? I don't believe I have ever seen such formatting. If this is not allowed, why mention that static methods cannot access members of an instance of a class?
Please help raise my confusion.
-DI
source to share
Accessing an instance member means accessing a field or attribute of an instance, not the instance itself, as this will not compile. Dot does not literally mean "access" exactly as you think, and I assume you have a source of confusion. The dot is used to call a method on a specific object (see link ) or to access an object's field (or class if the field is static).
For example, assuming the class is defined like this:
class MyClass {
private int x;
static void foo() {
... // foo cannot access member x
}
}
So, in a method, foo
you cannot reference x
, because it is an MyClass
instance-bound member field MyClass
.
Also see Understanding Class Members to understand the difference between static members and instance members.
source to share
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 with 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 must use the class name.
public class Example {
private static Object staticVariable;
public void instanceMethod() {
Example.staticVariable = null;
}
}
source to share
This suggests it:
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 cannot access an instance variable is because static refers to a class and not a specific instance of the class, so there is no instance to access the variable. The test will only exist when used new MyClass
, the test will now exist. But if I call a static method MyClass.getTest()
then no instance variable is created test
.
source to share