Created
October 9, 2011 14:10
-
-
Save php-coder/1273729 to your computer and use it in GitHub Desktop.
toString() methods examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Language { | |
private int id; | |
private String name; | |
private String locale; | |
public Language(final int id, final String name, final String locale) { | |
this.id = id; | |
this.name = name; | |
this.locale = locale; | |
} | |
public int getId() { | |
return id; | |
} | |
public String getName() { | |
return name; | |
} | |
public String getLocale() { | |
return locale; | |
} | |
public static void main(String [] args) { | |
System.out.println(new Language(1, "Russian", "ru_RU")); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.commons.lang.builder.ToStringBuilder; | |
public class Language { | |
... | |
@Override | |
public String toString() { | |
return new ToStringBuilder(this) | |
.append("id", getId()) | |
.append("name", getName()) | |
.append("locale", getLocale()) | |
.toString(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.common.base.Objects; | |
public class Language { | |
... | |
@Override | |
public String toString() { | |
return Objects.toStringHelper(this) | |
.add("id", getId()) | |
.add("name", getName()) | |
.add("locale", getLocale()) | |
.toString(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import lombok.ToString; | |
@ToString | |
public class Language { | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment