问题
I have been self-learning Java. I understand the scope of defining a class, but still didn't get the concept of an empty constructor usage.
Usually we should pass parameters for constructor to build instance object. But, I often see empty parameter for constructor. For example:
class Person {
String name;
int age;
public Person();
public Person(String name, int age){
this.name = name;
this.age = age;
}
}
I researched and read an example that using a class "fish" to explain. So, this is what I understood so far: when defining a class, we first define properties for an object, then we create a constructor that will build the object with methods. Empty constructor build GENERIC object, and constructor with parameters build objects with more specific information. Let’s say the example above, if I create an instance object using the empty constructor:
Person p1 = new Person();
-- it will still create an object but without any properties in it? So, what exactly the empty constructor is used for? I saw it in a lot of example codes. Is it very useful/common?
Thanks for looking and answering!
回答1:
There are three common reasons to define a default constructor:
- To construct an object with default values.
- To initialize an object that doesn't need parameters in that initialization process.
- To redefine the scope of the constructor. Making the constructor private will prevent anyone but the class itself from constructing an object.
回答2:
If you write a constructor with parameters, then you need to declare the default one (if you want to use it)
class Person {
String name;
int age;
public Person(String name, int age){
this.name = name;
this.age = age;
}
}
You can't do this now:
Person p = new Person();
In order to use the "default" constructor (with no parameters) you will need to declare it:
class Person {
String name;
int age;
public Person(){
name = "Man With No Name"; //sometimes you will want to set the variables
age = 21; //to some default values
}
public Person(String name, int age){
this.name = name;
this.age = age;
}
}
回答3:
I am sure other answers would come but in short -
Empty constructor just gives you an instance of that object. You might use setters on it to set necessary properties. If you want to make sure that any instance created is always valid and any member variables are always initialized,then you would define the constructor which initializes all the required member variables.
Also if you use frameworks like Spring, default constructor is often used where properties are set using setter injection.
回答4:
A lot of software written in java these days takes advantage of frameworks that operate directly on your code at runtime by using special JVM features (known as reflection) to just access your class directly and mess around with it, rather than access it through the code you actually wrote.
One common example of such a library is Hibernate. It "magically" takes information from a relational database and assigns the database values to the fields of an object. Your class is enhanced with additional code added at run time to make it magically map back to the contents of the database.
It is a consequence of the reflection API that it's a real pain in the butt to work with classes that do not have a 'default' constructor. You would have to supply a lot of additional information about how to correctly use your class's constructors in a way that can be programmatically interpreted.
Instead, the tools just require that classes have an empty, default, constructor, to make it easy to enhance the class at runtime.
Hence, we tend to leave an empty constructor always, even when creating an object without supplying certain values doesn't really make sense according to our program's API.
回答5:
An empty constructor usually is "a default"
Person() {
// This will cause you to not have any name or age values
}
So let's assume you want a default Person:
Person() {
this("Steven",20);
}
This way you will call the non empty constructor but you will always have the same person
回答6:
Whether you define an "empty" (default) constructor or not allows you to make clear to yourself and other developers, if certain fields are required to be set or not. (If the constructor demands them, they're required.)
Some libraries (for example Google GSON, ORMs ...) require you to have an empty constructor, but that's rather for technical reasons.
Another thing to keep in mind is that you can usually declare the fields assigned through a constructor as final, both for optimization and to make clear that they won't change during the object's lifetime.
But that all depends on the situation and what you're trying to do.
来源:https://stackoverflow.com/questions/18993936/how-to-best-explain-and-use-empty-constructors-in-java