5

This might just be a question of personal taste and workflow, but in case it's more than that, I feel I should ask anyway.

In Java, what differences are there between creating an instance via constructor and via a static method (which returns the instance)? For example, take this bit of code from a project I'm working on (written up by hand at time of posting, so some shortcuts and liberties are taken):

Plugin main;
Map<int, int> map;

public Handler(Plugin main) {
    this.main = main;
}

public static Handler init(Plugin main) {
    Handler handler = new Handler(main);
    handler.createMap();
}

public void createMap() {
    this.map = Maps.newHashMap();
}

In cases like this, what would the difference be between using

Handler handler = new Handler(this);

and

Handler handler = Handler.init(this);

in the Plugin class, besides the fact that createMap() runs only in the latter because it's not called in the constructor?

To clarify, in this case, Plugin is considered the main class.

I know enough Java syntax to be able to write intermediate-level plugins, but not enough about Java itself to know the difference between these two ways of doing this.

EDIT: For instance, the Maps class that I used to create the Map uses a static factory method (I hope I'm using that term correctly) called using the class instead of an object.

ChaoticWeg
  • 291
  • 2
  • 15
  • Sorry; SO posted before I was done typing it! Must have hit Tab by accident, and didn't realize it until I tried hitting Enter to insert a newline. But I'm glad that people were able to read and reply to it before I could finish typing it, though the info given at the time was almost nil B-) – ChaoticWeg Feb 27 '13 at 01:15
  • 1
    "written up by hand at time of posting" - you can always test code samples in [ideone](http://ideone.com/) (The "syntax highlighting" editor for them wouldn't be bad for creating those in the first place if it didn't insist on indenting with tabs.) – millimoose Feb 27 '13 at 01:34
  • @millimoose True. Although I meant something more along the lines of "if I accidentally fail to convey what I mean, it's because I'm writing this off the top of my head". But +1 for the suggestion :) – ChaoticWeg Feb 27 '13 at 03:38

2 Answers2

7

There are both advantages and disadvantages of static factory methods.

Advantages

  • Descriptive, meaningful names.
  • When invoked they can decide whether to return a new instance
  • They can return an object of any subtype of the return type
  • They reduce the verbosity of creating parameterized type instances

Disadvantages

  • If you provide only static factory methods, classes without public or protected constructors cannot be subclassed
  • They are not readily distinguishable from other static methods

Source: Effective Java, Second Ed.

6

The difference is a static factory method is more flexible. It can have all sorts of ways to return an instance. It can do other side stuff. It can have a more descriptive name. It can be invoked by its simple name (e.g. foo(args)) by static import or inheritance.

The constructor call is more certain - the caller knows exactly what's happening - a new instance of that exact class is created.

irreputable
  • 44,725
  • 9
  • 65
  • 93
  • +1. I would add that a static factory method has an option to return an instance of a different concrete class, which can be very useful in some circumstances – mikera Feb 27 '13 at 01:39
  • Accepted. This is the type of answer I'm looking for. Nishant's answer is indeed helpful and informative, but I'm looking more for differences than advantages/disadvantages. Either way, both are helpful. :) Thanks! – ChaoticWeg Feb 27 '13 at 15:13