Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

Sometimes when implementing a constructor on an object that requires calling another constructor there's a need to call a helper method that does some work. This is done either for convenience or because some work needs to be done on input parameters before calling the super/this constructor. In these situations you're likely to come across an error like this - "Cannot reference 'X' before supertype constructor is called".

What does that mean exactly? Well since we are inside a constructor and the object is not yet created, it is impossible to call instance methods. The object doesn't exist so the method doesn't exist yet!

So how do we get past this? Since an instance method is not allowed we need to look at a different way of implementing our helper method. The solution - make the helper method static. Since static methods do not depend on an object being instantiated, we can call them inside the constructor even before the object is created.

This will work if the helper method doesn't rely on any member variables or other instance methods.

I've recently used this approach in jPhotoFrame. I had a situation where my object had multiple constructors and in one of the constructors I had to extract a value from a JSON object and then pass it to different constructor. The second constructor invoked the parent constructor which set all the member variables from its inputs.

I could have of course just duplicated my code for setting the member variables but decided it was cleaner to do it this way.


Have comments or feedback on what I wrote? Please share them below! Found this useful? Consider sending me a small tip.
comments powered by Disqus
Other posts you may like...
Hi! You can search my blog here ⤵
Or browse the recent top tags...

Recent Blog Posts

How to fix Google Cloud SDK dev server error - No module named ipaddr

Adorable but totally metal - Metal Earth 3D Guardians of the Galaxy Groot model kit

Riverside Expressway Cam shut down permanently

Inserting Google DFP ads with Backbone, Underscore and jQuery

How to resolve the domain is already mapped to a project error in Google App Engine

A quick look at the Nyko Super MiniBoss wireless controllers for the SNES mini

Loading and displaying a collection from bootstrapped data in Backbone.js

Add this handy function to your Bash profile file to display the compiled JDK version for a .class file

How does PCBWay stack up as a low budget PCB fab

Resolving the Cannot reference X before supertype constructor is called compiler error in Java

Recent Galleries

BMB-012 Nanoblock T-Rex Skeleton Model assembly

Tiny Arcade revision 6 kit assembly and decal application

Atari Lynx repair - Part 5 - McWill LED screen mod installation

Atari Lynx repair - Part 4 - screen cover replacement

Atari Lynx repair - Part 2 - re-capping the motherboard

Atari Lynx repair - Part 3 - broken speaker replacement

Atari Lynx repair - Part 1 - introduction and case disassembly

Building a custom Atari Lynx game box storage shelf unit in a day

Protecting old Atari Lynx game boxes with snug fit plastic sleeves

Monument Valley 2 is released and does not disappoint

Blogs and Friends

Matt Moores Blog
Georgi's FlatPress Guide
Perplexing Permutations
The Security Sleuth
Ilia Rogatchevski
Travelling Fairy

Blog Activity

Blog Activity