So my understanding was that you can't use static method to access non-static variables, but I came across 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 isn't
public static void repair(Laptop laptop) {
laptop.memory = "2GB";
}
accessing String memory defined in class Laptop, which is non-static instance variable?
Since the code compiles without any error, I'm assuming I'm not understanding something here. Can someone please tell me what I'm not understanding?