Is it OK to write a constructor which does nothing?
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() {
}
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.
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.
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
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.
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.
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.
It is absolutely OK to do it this way. May be you will fill it later with some useful code.
You don't need to write an empty constructor (because it's the default) unless you have other constructors with parameters.
There's no need to specify the default (empty) constructor, as the compiler creates it for your automatically.
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.
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.
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.
If you do not need a constructor, then you probably do not need to instantiate this class at all. If all you want the class to do is provide methods for you to use, why not just make it a static class?
精彩评论