Static Methods:
- Static methods cannot use the 'this' keyword
- Static methods can only access static members
- Instance methods can access both static and instance members
Static methods do not have object references because they are loaded with the class and exist independently of any object instance. If you attempt to call an instance method via the class name without creating an object, it would be invalid since instance methods require an object reference. Instance methods internally use 'this', which represents the object's memory address. Since static methods belong to the class rather than any particular instance, they're called directly using the class name.
package com.itheima.supplement;
public class Student {
String name;
int age;
static String teacherName; // Static member variable
// 'this' refers to the current object's memory address
// This 'this' can be thought of as a Student type variable provided by the JVM
public void displayInfo(){
System.out.println("this:" + this);
System.out.println(this.name + "," + this.age + "," + teacherName);
// Calling other methods
displayDetails(); // Implicitly calls this.displayDetails()
process(); // Implicitly calls this.process()
}
public void displayDetails(){
// Implementation here
}
public static void process(){
System.out.println("Static method");
}
}
public class StudentTest {
public static void main(String[] args) {
Student.teacherName = "Teacher Wang";
Student student1 = new Student();
System.out.println("student1:" + student1);
student1.name = "Zhang San";
student1.age = 23;
student1.displayInfo();
System.out.println("==========");
Student student2 = new Student();
System.out.println("student2:" + student2);
student2.name = "Li Si";
student2.age = 24;
student2.displayInfo();
}
}
The actual signature of the instance method displayInfo() is essentially:
public void displayInfo(Student this)
The hidden 'this' parameter is automatically assigned by the JVM during method invocation. When calling displayDetails(), it actually executes:
this.displayDetails();
The 'this' keyword is implicitly hidden, meaning the same object that invoked displayInfo() continues to call displayDetails(). If we try to access 'name' within the static method process(), it will cause a compilation error because the compiler doesn't know which object's name property to reference. If we want to access a specific object's name property, we'd write:
System.out.println(name);
This effectively becomes this.name, referring to the name property of a sepcific object. However, static methods don't have access to 'this' (static methods are shared among all instances, not tied to individual objects), so the system throws an error.
Alternative perspective: 'name' is an instance variable, which belongs to individual object instances. Since no object has been created yet, there's no way to access instance variables through a static context.
System.out.println(this.name);
Similarly, attempitng to call instance methods from within static methods is impossible:
displayInfo();
This effectively translates to this.displayInfo(), which is also invalid since static methods can only access static members.