Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a method append(String) onto itself in Java?

What I am trying to do is use method().method() in the following code:

public class Practice {
    public static void main(String[] args){
        Message m = new Message("test");
        m.append("in").append("progress").append("...");
        m.printMessage();
    }
}

My class Message is this:

public class Message {

    private String astring;

    public void append(String test) {
        astring += test;
    }

    public Message(String astring) {
        this.astring = astring;

    }
    public void printMessage() {
        System.out.println(astring);
    }
}

How can I use .append().append()?

like image 542
Igazi Avatar asked Mar 10 '23 12:03

Igazi


1 Answers

Change the method to the following:

public Message append(String test) {
    astring += test;
    return this;
}
like image 94
Jacob G. Avatar answered Mar 12 '23 02:03

Jacob G.