Can static methods be used to make the code more readable?
If so, how could a class attribute be changed with a static method?
public class Coche {
private ListaAverias la;
private listaPiezas piezas;
private String color;
private boolean roto=false;
public void update() {
pintarPiezas(piezas,color);
arreglarAveria(la);
destrozar(roto);
}
public static void pintarPiezas(listaPiezas piezas,String color){
mucho codigo...
}
public static void arreglarAveria(ListaAverias la){
mucho codigo...
}
public static void destrozar(boolean roto){
roto=true;
}
Static, or class, methods and attributes do not exist to make the code more readable.
Attributes of this type, as they belong to the class itself and not to a specific instance, allow all instances of the same class to share the value of this attribute. Which means that in an instance you
a
modify the value of a static attribute, an instanceb
will also see this change reflected, since it is really done at the class level.For this reason, and although it can be done, it is more advisable to access these attributes through the class and not through a specific instance.
Test.num = 7;
In the case of methods, since they belong to the class itself, they are not allowed to manipulate instance attributes (those that are not static) since the VM would not know in which of all the instances to modify said attribute.
For all that said, it is NOT correct to declare the attributes of a class as static if they must store different values in each of the different instances of said class. In the case of methods, the same thing happens, it is NOT correct to declare them as static if they modify or manipulate the state of the specific instance (this is that it requires the manipulation of NON-static attributes for its operation).
Static methods can only read/write static attributes, not instance ones. If you want the method to be able to make any modifications to an instance attribute, you'll need to add an additional parameter so that it references the current object.
For example:
In that example it will give a compilation error, since there is no reference to the object. It would not be possible to assign a 10 to the broken variable, since we need to know in which memory address it is located. Now with this example, I'm going to prove it to you.
Imagine that our object is designed like this in RAM:
In order to be able to read/write to the attribute
roto
(which is actually the address0x08
), I would need a pointer that has a reference to the base address of the Car object, that way I could scroll to the address0x08
and assign the respective data.This is the main reason why the above example would not work because we need to have an implicit pointer as a parameter (in Java there are no pointers, but internally they do) that refers to the base address of the instantiated object.
The corrected code would be like this:
The parameter
coche
is the implicit pointer or instance variable that will help you to have the reference of the Car object and thus be able to make any modification to a non-static attribute.