Verify that the attributes of an object are null in a less manual way

3

How to fix this:

Objeto obj = new Objeto();

if(obj != null){
  //executa o codigo
}

The question is: did I instantiate the object, beauty? But I did not set any value for it so all its attributes are empty.

If I make a comparison to see if the object is null, it will return me false. How do I check if all attributes of the object are null or if the object is null after being instantiated without having to exit by checking each attribute of the object?     

asked by anonymous 10.03.2016 / 04:55

2 answers

4

The most appropriate way to ensure that the attributes of an object are properly defined is by forcing them to initialize at startup.

It is a good practice for all attributes of an object to be final . final attributes must be initialized until the constructor of the object finishes.

Example:

public class MeuObjeto {
    private final String atributo1;
    private final int atributo2;
    public MeuObjeto(String atributo1, int atributo 2) {
        this.atributo1 = atributo1;
        this.atributo2 = atributo2;
    }
    //getters
}

If, for some reason, your object can not be immutable, then you will not be able to use final , but you can still force all attributes to be initialized using the constructor.

public class MeuObjeto {
    private String atributo1;
    private int atributo2;
    public MeuObjeto(String atributo1, int atributo 2) {
        this.atributo1 = atributo1;
        this.atributo2 = atributo2;
    }
    //getters
    //setters
}

However, keep in mind that mutable objects are a common cause of problems in many scenarios and not just for competition as you might think.

All this "problem" described in the question, related to having an object with undefined state, actually exists only because of a "weak" model of objects that allows them to arrive in this state.

For each scenario there is a different strategy for modeling an object well and the general rule is, avoid leaving changeable what does not need to be changed.

    
10.03.2016 / 05:27
2

There is no way.

Think about it: how would Java know its object is "null"? The null you are proposing is semantic , that is, it depends on your interpretation of the object data. Let's say that the object class in question has an attribute of type int with the value 0 at a given moment. Does this 0 mean "no value" or the presence of a specific value, which happens to be 0 ?

For this reason, there is no way for Java to provide this kind of thing for you - it has no way to read your mind (or description of your requirements).

What can be done is to use a method in the class that will do this check. As it is a method, you will not need to worry about code repetition: it will only write it once. This happened to the String class, which gained a isEmpty method.

So, make a isEmpty in your class Objeto and use it like this:

Objeto obj = new Objeto();

if(obj && !obj.isEmpty()){
  //executa o codigo
}

The operation of isEmpty() , of course, will depend entirely on your concept of Objeto empty.

    
10.03.2016 / 05:10