Does it make sense to declare a default constructor in Java?

Does it make sense to declare a default constructor in Java?

class MyClass { public MyClass(){} public MyClass( T someArgs){ //somecode } } 
+4
source share
5 answers

Yes, when you have other constructors.

  • This is required when you want to create objects without passing any parameters for the constructor.

  • In most cases, this is required to reflect, especially when you work with a certain basis on reflection of the structure and the library (for example, serialization, Hibernate, Hessian).

  • Using setters instead can usually give you better control. This also works well with inheritance (where you will need to explicitly call the constructor for all specialized classes, since the constructors are not virtual.

+6
source

If you do not have a default constructor, the default constructor is implicitly available. It is a good idea not to set, if this is your goal - not to do this:

 MyClass myClass = new MyClass(); 
+8
source

Java adds a public no-args constructor by default if you do not specify any other constructors, so there is one point when setting it, if you need it, and specify another. Therefore, in your case, if you defined your class as follows:

 class MyClass { public MyClass( T someArgs){ //somecode } } 

then you could not do this:

 MyClass a = new MyClass(); 

but if it was defined as follows:

 class MyClass { // no constructors } 

could you.

Often no-arg constructors are used to specify pseudo-data arguments. For instance:

 class Switch { private boolean on; public Switch() { this(true); } public Switch(boolean on) { this.on = on; } public boolean isOn() { return on; } public boolean toggle() { on = !on; } public boolean set(boolean on) { this.on = on; return this.on; } } 
+6
source

In addition to all the previous arguments for the no-args constructor, it is a required element of JavaBeans, since these beans can be created using reflection (in particular, by calling Class.newInstance ). As a result, any JavaBeans-based framework will make this no-args constructor an essential part of your architecture.

There is also an added benefit, as the no-args constructor can help create some kind of free interface, allowing you to call setters. For example, in the company I worked for, it is used to determine next to setters and getters with the following methods:

 class MyClass { privaite int index; public int getIndex() { return index; } public void setindex(int index) { this.index = index; } public MyClass withIndex(int index) { setIndex(index); return this; } } 

Lets me create objects as follows:

 MyClass myObject = new MyClass().withIndex(2); 

This was a very useful way to create objects without defining error constructors.

+3
source

Some frameworks require a constructor with a null argument / by default. For example, you need to run the class as a JUnit test case. [edit: invalid statement deleted]

This is due to their use of reflection.

0
source

Source: https://habr.com/ru/post/1303919/


All Articles