Over a million developers have joined DZone.

Two Useless Java Customs

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

A couple common Java practices that annoy me.

Calling super() in a constructor

If you use Eclipse to generate a constructor it will look something like this:

public MyClass(int x) {
this.x = x;

Eclipse always adds the call to super(). PMD has a rule for this (in its Controversial Rules category). The explanation is simply “It is a good practice to call super() in a constructor”.

If you omit super() in a constructor the compiler adds the call to super() for you. The compiled code is identical, whether you write the call or not. Effective Java doesn’t seem to address this. Is there any benefit to writing the call in all your constructors?

Supplying messages to JUnit assert calls

JUnit assert methods support a message argument, displayed if the test fails:

assertEquals("a != b", a, b);

This message does make a test failure slightly easier to read but I don’t believe the benefit outweighs the tedious overhead of typing messages for all your asserts. Tests should fail rarely, and when they do they almost always require stepping through or at least looking at the source of the test to figure out why they are failing.

I always omit the message unless it’s explaning something that isn’t obvious when looking at the code.


From http://dmy999.com/

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}