Check whether the attributes of an object are null in a less manual way

Asked

Viewed 3,331 times

3

How to solve this:

Objeto obj = new Objeto();

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

The question is: have I instated the object, beauty? Only I have 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 object attributes are null or if the object is null after being instantiated without me having to check each object attribute?

2 answers

4

The most appropriate way to ensure that the attributes of an object are properly defined is to force their initialization during the initialization of the object.

It is good practice that all attributes of an object are final. Attributes final must be initialized until the object constructor is finished.

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 cannot be immutable, then you will not be able to use the final, but 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 common cause of problems in various scenarios and not only for competition as is often thought.

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

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

2

There’s no way.

Think about it: how would Java know that its object is "null"? The null you are proposing is semantic, i.e., 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 time. That 0 means "absence of value" or the presence of a specific value, which happens to be 0?

For this reason, there is no way Java can provide this kind of thing for you- it cannot read your mind (or the 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: you will only write it once. This happened with the String class, which won a method isEmpty.

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 functioning of isEmpty(), of course, it will completely depend on your concept of Objeto emptiness.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.