0

I have read a lot about final and when to use it or not ,and I have come to the conclusion that it's mostly personal design. So, consider this class below, is this a case where I'm overusing final or not? I know that I should write readable and clear code ,and I think this still is pretty clear consider all the final.

public final class ConsoleUtil {

private static final Logger LOGGER = Bukkit.getLogger();

private ConsoleUtil() {

}

public static void info(final Object... objects) {
    log(Level.INFO, objects);
}

public static void warning(final Object... objects) {
    log(Level.WARNING, objects);
}

public static void severe(final Object... objects) {
    log(Level.SEVERE, objects);
}

public static void exception(final Exception e, final Object... objects) {
    warning(objects);
    e.printStackTrace();
}

private static void log(final Level level, final Object... objects) {
    for (Object object : objects) {
        LOGGER.log(level, "[HubControl] " + object.toString());
    }
  }

}

Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
Isak
  • 55
  • 9

1 Answers1

0

Your conclusion that the choice is mostly personal is mostly correct, so you should not really be asking the question, because the answer is off topic as "primarily opinion-based".

I will try to give you an answer which is as far from opinion-based as possible.

Finality is a most excellent quality for any data item, and any opportunity to make something final should be pursued tenaciously. On the other hand, with java, it becomes extremely tedious to add final to every single little thing that can be made final. This also becomes kind of futile given that the compiler analyzes your code and can figure out whether a variable is effectively final when finality actually matters.

So, the approach taken by most java developers is to refrain from using final all over the place. Note that this is not subjective, it is a report of the state of the industry.

We could ask the gurus at Oracle to add finality-by-default to the language, so that everything is final unless declared otherwise, or we could use a different language where finality-by-default is already built in.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142