this Keyword

Inside the constructor, as well as in other methods belonging to the object, a special keyword called this can be used. This keyword is a reference to the current instance of the class.

The this keyword is useful when the class

Calling a Method with this keyword

You can call another method within the same class using this. Within the send method, we call the isConnected method and then call connect method.

public class Socket{
    boolean isConnected(){
        //more code here
        return true;
    }

    void connect(){
        //code to connect to server
    }

    void send(String message){
        if(!this.isConnected()){
            this.connect();
        }
    }
}

results matching ""

    No results matching ""