0

Is there any way to change how an Object represents itself as a String?

For example, having System.out.println(new JLabel()); output Empty JLabel.

1
  • 2
    Not on types you don't control. Commented Sep 1, 2013 at 0:23

3 Answers 3

2

You need to override the toString() method.

@Override
public String toString() {
    return "Empty JLabel";
}
Sign up to request clarification or add additional context in comments.

Comments

0

Yes. Override the toString() method for that class

Comments

0

you could override the toString method like this:

public class A {

    @Override
    public String toString() {
        return "Empty A";
    }

    public static void main(String[] args) {
        System.out.println(new A());     // Empty A
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.