Introducing $ – the Java Static Constructor

So this one will probably be controversial.

I’ve been playing around with this idea for a while, really since I started using Ruby in a hobby capacity.  When instantiating a variable in Ruby, let’s say a simple 2D point class, you would use the following syntax:

Whereas we all know that the equivalent Java instantiation would be

There’s something I like about that static style constructor in Ruby, especially when constructing a method argument.

seems a little… neater… than

This is more apparent when instantiating multiple method arguments together

I don’t like the disjointed nature of that whitespace between new and Point2d.

I’ve started adding a static constructor (or factory method), along the lines of Ruby’s .new for those small, struct like classes that are basically just data objects and don’t make sense without state.

Obviously new is a reserved keyword so I had to come up with something else, and my search through the thesaurus yielded no help.  All the other options were just a bit too long, or non-obvious.

I decided to go a different direction. As we all know, identifiers can start with underscores, letters or currency symbols. I have chosen to give my static constructor a short, sharp and instantly identifiable name.


Just a single character, for maximum brevity. Sure it’s only by convention, but it’s instantly recognizable, once you know what it is. It reads nicely when used in code, especially when part of a method call.

I don’t make the default constructor private, so it’s still there for others to use. I consider this static constructor a nice addition, use it if you want to, ignore it if you don’t.

Here’s the implementation in Point2d

And usage

Obviously it doesn’t make sense for every class, and is one of those things that’s best used conservatively, but I have to say I like it, and am going to use it for a while to see how it goes.

What do you think? Am I crazy? Would you hate to see this? Do you like it?

Leave a comment and let everyone know what you think!


So the discussion on Reddit and below generated a load of interesting points and counterpoints. One of the ideas was to call the static constructor ‘of’ instead of ‘$’ which is a great suggestion, especially considering another commenter pointed out that using $ goes against the Java language specification, oops!

Using ‘of’ is also consistent with Java 8 Optional.

Another comment mentioned that something like this is best done on immutable value types, and I couldn’t agree more, so ignore the default constructor and setters in the example above!

I knew this one would be a bit controversial, but all of the comments and feedback were in good spirit and the discussion was very enlightening, so thanks everyone for a great time!

In conclusion I think this one might go back onto the drawing board for a while and maybe if I feel the need in future I’ll go along the lines of using .of() .

9 thoughts on “Introducing $ – the Java Static Constructor”

  1. Hello, thanks for sharing your idea!

    My personal opinion is that ‘$’ is better replaced with a word that describes what’s happening. Most of the time when I see these types of static constructors, the word used is ‘create’, e.g. ‘Point2d.create(0.1, 0.2);’

    1. Hi Lain, thanks for taking the time to comment, yes I started out with ‘create’ as the name of the method, but found that it was pretty clunky in method parameters. It’s definitely more descriptive though, that’s for sure!

  2. Well, it’s a sort of code pollution. It also promotes potential static imports clashing (don’t think you’re a fan of static imports though). Why not just create a static factory method (not even Point.create) named point2D so you could have beatuful asList(point2d(1,1), point2d(2,2), point2d(3,3)) ?

Leave a Reply

Your email address will not be published. Required fields are marked *