views:

392

answers:

13

To use methods of a class I need to instantiate a class. At the moment the class has not constructor (so I want to write it). But than I have realized that the constructor should do nothing (I do need to specify values of fields).

In this context I have a question if it is OK to write constructor which does nothing. For example:

public Point() {
}
+14  A: 

You don't need to write an empty contstructor; the Java compiler will automatically insert one for you (only if you haven't defined any other constructors that take some args).

So it's quite okay (and useful in some situations) but it's not required.

Noon Silk
I would say do not insert empty constructors as you are just adding noise. It doesn't do anything and it is there occupying space on the screen :-).
Ravi Wallau
+4  A: 

It sounds like you are explicitly creating a default constructor which there is no need to do. The Default Constructor is automatically created if there are no other constructors.

Matt Dearing
A: 

It is absolutely OK to do it this way. May be you will fill it later with some useful code.

alemjerus
A: 

You don't need to write an empty constructor (because it's the default) unless you have other constructors with parameters.

kukudas
+1  A: 

A constructor that does nothing is certainly acceptable by the language, since Java provides one by default if you don't (JLS 8.8.9):

If a class contains no constructor declarations, then a default constructor that takes no parameters is automatically provided.

There are some restrictions on exactly when the default constructor can be automatically provided, what access modifier it will have, etc, and it's all specified in JLS.

polygenelubricants
A: 

There's no need to specify the default (empty) constructor, as the compiler creates it for your automatically.

Olli
A: 

Sure, if you don't do it, compiler will do, however as it is a part of language I would go for no ctor unless it should be private or have arguments.

Gabriel Ščerbák
+3  A: 

You don't need to. Java will provide you with a default constructor if you do not specify one:

If a class contains no constructor declarations, then a default constructor that takes no parameters is automatically provided

Luhmann
+1 for the reference link!
FrustratedWithFormsDesigner
A: 

Of course you can. If you don't provide, Java usually create and Anonymous Inner Constructor for you.

This is very evident when creating a getter and setter bean where there is no constructor but there exists an anonymous constructor.

For more information, visit the (former Sun) Oracle Site here: http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#252986

The Elite Gentleman
And how is this wrong? Obviously this person doesn't know about empty constructors.
The Elite Gentleman
It's not called anonymous inner anything, it's called a default constructor.(downvote wasn't mine, btw)
Jorn
I know that Jorn, check the link about Anonymous classes. I've included it in my post.
The Elite Gentleman
A: 

As others have mentioned, in your case a default constructor is not necessary because Java will create a default constructor when you don't define it. But to answer your question it is completely ok to have an empty default constructor.

Another case where you need a default constructor is when you constructor(s) with parameter(s) and your class needs to be serialized.

jpartogi
+2  A: 

As everyone else has said, you don't need one for it to compile.
But for coding standards I'd throw it in there. When looking at a class generally the constructor is at the top and it's nice to see what happens. If its not there the developer would need to search the class for it. And depending which IDE is being used, it might be a pain.

But its really up to you.

Brad8118
I don't really see how it is useful relatively to coding standards : it clutters the source unnecessarly, and gives no information.
Valentin Rocher
If your coding standards say "put the constructor at the top" then why would you need to search the class if you don't see a constructor at the top?
Joel Mueller
A: 

If you are going to have a class that needs an empty constructor, and later constructors with arguments I would say it is good in a defensive style of coding to go ahead and include it. Since the empty constructor won't be put there by Java if there are other constructors with arguments.

It's sort of like if you have an if statement that only has 1 statement under it. Should you go ahead and put braces around it, or simply just leave the one statement indented under the if? Well if you add more statements under that if later and forget the brackets you have a logic error there. Might as well go ahead and put the brackets, or in other words the constructor.

AFK
+2  A: 

As far as I know, the only actual use for an empty constructor is when you make it private to prevent anybody from instantiating the class, like so:

private Point() {}

Otherwise, as the other answers have said, it's purely a stylistic choice, fundamentally harmless either way.

BlairHippo