Java preconditions about class variables

138 Views Asked by At

i have a question about preconditions. I have this code.

public class NetModel() {
    private Net net = null;
    
    public NetModel() {}
    
    public void setNet(Net net) {
        this.net = net;
    }
    /**
     * @param netName the name of the net
     * preconditions : netName is not null
     */
    public void workWithNet(String netName) {
        net.setName(netName);
    }
}

The Net class has a method to set a name for nets. I want to assure the user that if he passes a non-null string, the method will work fine. So, my question is, have I to include in the precondition that the class variable 'net' has to point to an instance of a net? Like this

/**
 * @param netName the name of the net
 * preconditions : netName is not null and class variable 'net' has to be initialized by the 
 *                 user with the method 'setNet(Net net)'
 */
 public void workWithNet(String netName) {
     net.setName(netName);
 }

Or is not the user that has to handle it? How I have to handle possible problems with class variables preconditions in a contract with the user? Thank you.

0

There are 0 best solutions below