So, I realized that you cannot use the static method to access non-stationary variables, but I came across the following code.
class Laptop {
String memory = "1GB";
}
class Workshop {
public static void main(String args[]) {
Laptop life = new Laptop();
repair(life);
System.out.println(life.memory);
}
public static void repair(Laptop laptop) {
laptop.memory = "2GB";
}
}
Which compiles without errors.
So don't
public static void repair(Laptop laptop) {
laptop.memory = "2GB";
}
access to string memory defined in the Notebook class, which is a non-static instance variable?
Since the code compiles without any error, I assume that I do not understand anything. Can someone please tell me what I do not understand?
source
share