You cannot reference class/instance variables from a static method because a static method is not associated with a specific instance of its class. You are referring to name
inside a static method, but for example, if I say
MyClass.create()
I did this without instantiating an object of type MyClass
, so without instantiation, Java does not reserve any space in memory for the name
variable
Static methods are meant to be used as helper or util methods.
Also, it is odd that you have a method called create
Creation is referred to as instantiation, and it is the job of the constructor
to handle this logic.
You can try this in your class that is invoking this create
method
TapeObject to1;
if (format == "w") {
to1 = new Wall("Berlin Wall")
}
Your Wall
constructor should be as follows:
public Wall(String name) {
super(name); // passes the name field to your superclass
}