Always Remember to override toString() [Effective Java]

5 10 2013

Default implementation of toString() in Object class is class name, ‘@’ sign and hash code. All of it is not enough when you want to print or debug object state if the class is used by you. Even worse, if your class is used by external developers with API you provide.

How to do it in eclipse?

No – you don’t have to implement it by yourself :) Eclipse is here for help. To override toString() simply:

  • in Eclipse editor right click -> Source -> Generate toString()…

or with shortcuts

  • in editor press Alt + Shift + S -> press ‘S’

What you need to remember when overriding toString() manually?

  • Return as much information as needed (that may be interesting)
  • It is obligatory in data classes
  • if you decide that your toString() provide result in format presentable to the user, then you have to clearly document output print format and remain it unchanged for life. In that case you need to be aware that toString() output may be printed in UI somewhere
  • beside toString() you still need to provide accessor methods for class fields, if needed

Effective Java

Effective Java (Joshua Bloch) is a book with advanced Java programming techniques that make Java code more robust and readable. I am currently reading it and this is why these [Effective Java] posts come up here. These are summaries of chapters that are most valuable in my opinion.

Did I help you?
I manage this blog and share my knowledge for free sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!





Three ways of implementing singleton [Effective Java]

28 09 2013

Singletons is a well known pattern. No matter whether you like or dislike it, it happens to be useful. Joshua Bloch in “Effective Java” reminds about three methods to implement it. One of them is not obvious, but quite interesting (Singleton as Enum)

1. Member as a public final field

public class Elvis {
	public static final Elvis INSTANCE = new Elvis();

	private Elvis() { ... }

	public void leaveTheBuilding() { ... }
}

Here the private constructor is called only once to initiate public static final field. Once it is initialized it reminds unchanged. But best practice tells to use accessor methods:

2. Singleton with static factory

This is probably the most popular implementation of singleton pattern:

public class Elvis {
	private static final Elvis INSTANCE = new Elvis();
	private Elvis() { ... }
	public static Elvis getInstance(){ return INSTANCE; }
	public void leaveTheBuilding() { ... }
}

Here also the private constructor is called only once, and getInstance() method is an accessor to private static final field and returns always the same reference.

The advantage here over first case is that such accessor clearly indicates that this is a singleton, not a public constant

3. Enum Singleton

This is the most interesting way to implement singleton – simply create Enum type:

public enum Elvis {
	INSTANCE;

	public void leaveTheBuilding() { ... }
}

This approach is available since Java 1.5 (when Enums where introduced). It is not widely used, however it is the best way to implement singleton, according to the author, because it:

– is concise

– provides serialization for free

– gives 100% guarantee that INSTANCE will never have another instantiation (even in case of sophisticated serialization and reflection attacks)

I would only recommend to name this class ElvisSingleton instead of Elvis just to clearly indicate that this is singleton.

Effective Java

Effective Java (Joshua Bloch) is a book with advanced Java programming techniques that make Java code more robust and readable. I am currently reading it and this is why these [Effective Java] posts come up here. These are summaries of chapters that are most valuable in my opinion.

Did I help you?
I manage this blog and share my knowledge for free sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!





Builder instead of constructor with many (optional) parameters [Effective Java]

21 09 2013

What is it?
Builder is an internal class that is dedicated to build an instance of a type.

When to use it?
It is useful when you have type with optional parameters (that are not always mandatory to provide when creating object). The difference is significant when there is a plenty of params. Then instead of invoking constructor with not all params required:

Type obj = new Type(param1, param2, null, null, null, param6, null);

You can invoke Builder that will distinguish mandatory params from optional ones:

Type obj = new Type.Builder(param1, param2).setParam6(param6).build();

Here just param1 and param2 are mandatory, the rest is optional.

Advantages:
– the code is easier to read and more robust
– good way to cope with many optional construcotr parameters

Effective Java

Effective Java (Joshua Bloch) is a book with advanced Java programming techniques that make Java code more robust and readable. I am currently reading it and this is why these [Effective Java] posts come up here. These are summaries of chapters that are most valuable in my opinion.

Did I help you?
I manage this blog and share my knowledge for free sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!





Object construction: static factory method instead of constructor [Effective Java]

14 09 2013

What is it

Static factory method is a public static method that is used to create object – exactly the same as constructor. So when it is better than usual constructor?

Advantages

– unlike constructors, they have names, so you can easily distinguish and know what kind of object they actually build

– unlike constructors, they do not have to create new instance each time they are called, so in other words you can easily make this class a singleton

– they can return an object of any subtype of their return type

– they reduce verbosity of creating parameterized type instances. So instead of header:

public Bread(boolean old);

you can use

public static Bread createOldBread();

and

public static Bread createFreshBread();

Disadvantages

– A class without public or protected constructors can’t be subclassed

– static factory methods are not easily distinguishable from any other static methods, so good naming is required

Effective Java

Effective Java (Joshua Bloch) is a book with advanced Java programming techniques that make Java code more robust and readable. I am currently reading it and this is why these [Effective Java] posts come up here. These are summaries of chapters that are most valuable in my opinion.

Did I help you?
I manage this blog and share my knowledge for free sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!








%d bloggers like this: