false_azure
false_azure

Reputation: 1503

Java - is it bad practice not to have a class constructor?

I want to make a helper class that deals with formatting (i.e. has methods to remove punctuation and convert between types, as well as reformatting names etc.). This doesn't seem like it will need any fields - its only purpose is to get passed things to convert and return them, reformatted. Is it bad practice to leave out a constructor? If so, what should my constructor be doing? I was looking at this link and noticed that the class it describes lacks a constructor.

Upvotes: 14

Views: 6174

Answers (7)

Kurt Hoppe
Kurt Hoppe

Reputation: 11

You don't have to provide any constructors for your class, but you must be careful when doing this. The compiler automatically provides a no-argument, default constructor for any class without constructors. This default constructor will call the no-argument constructor of the superclass. In this situation, the compiler will complain if the superclass doesn't have a no-argument constructor so you must verify that it does. If your class has no explicit superclass, then it has an implicit superclass of Object, which does have a no-argument constructor.

Java Official Document: Providing Constructors for Your Classes

Upvotes: 0

Narendra Pathai
Narendra Pathai

Reputation: 42005

Any class that has all the methods which do not have or need any state is free to reduce the visibility of constructor by making the constructor private.

Example java.lang.Math class in Java.

As java.lang.Math has all static methods which do similar job as your class they have declared the constructor as private so that nobody can accidentally create the instance of that class.

    /**
     * Don't let anyone instantiate this class.
     */
    private Math() {}

Upvotes: 6

Nomesh Gajare
Nomesh Gajare

Reputation: 855

no its good to leave out a constructor as there aren't any instance variables in your class!

constructors are meant to initialize the instance variables!

still if you skip the constructor, compiler anyways inserts the default constructor which is fair enough!!

Upvotes: 1

Gab
Gab

Reputation: 8332

Compiler will generate a default constructor (with no parameters) for you. If your class has not state and does not extend a class which needs initialization, you can let it without declaring explicit constructor

Upvotes: 1

Kshitij Jain
Kshitij Jain

Reputation: 1793

Usually it is a good coding practice to define your constructor in the class though each class has a default constructor .

But if you do not have any special need to use a oveloaded constructor or to make any singleton pattern then you can remove the constructor .

If you are using static methods in your case then also you dont have any need to define constructor , as you do not need to have object of this class .

Upvotes: -2

kishu27
kishu27

Reputation: 3120

Not bad practice. but the example that you have given doesn't have any member variables that can be used in an Object context. In such situations, it's best to have static methods because then you don't need to allocate memory to create objects for the class before calling the methods.

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1504122

Is it bad practice to leave out a constructor?

Yes - because unless you specify any constructors, the Java compiler will give you a constructor with the same visibility as the class itself.

Assuming all your methods are static - which seems likely unless you want polymorphism - you should make your class final and give it a private constructor, so that other developers don't accidentally create an instance of your class, when that would be pointless. When thinking about an API, any time I can remove the ability for developers to do something stupid, I do so :)

So something like:

public final class Helpers {
    private Helpers() {
    }

    public static String formatDate(Date date) {
        // etc
    }
}

Note that by taking polymorphism out of the equation, you're also removing the possibility of changing this behaviour for tests etc. That may well be fine - I'm not a believer in "no statics, ever" - but it's worth considering.

Upvotes: 41

Related Questions