Java Default Constructor

Java default constructor

Neither of them. If you define it, it's not the default.

The default constructor is the no-argument constructor automatically generated unless you define another constructor. Any uninitialised fields will be set to their default values. For your example, it would look like this assuming that the types are String, int and int, and that the class itself is public:

public Module()
{
super();
this.name = null;
this.credits = 0;
this.hours = 0;
}

This is exactly the same as

public Module()
{}

And exactly the same as having no constructors at all. However, if you define at least one constructor, the default constructor is not generated.

Reference: Java Language Specification

If a class contains no constructor declarations, then a default constructor with no formal parameters and no throws clause is implicitly declared.

Clarification

Technically it is not the constructor (default or otherwise) that default-initialises the fields. However, I am leaving it the answer because

  • the question got the defaults wrong, and
  • the constructor has exactly the same effect whether they are included or not.

Do I really need to define default constructor in java?

A default (no-argument) constructor is automatically created only when you do not define any constructor yourself.

If you need two constructors, one with arguments and one without, you need to manually define both.

What is responsible for generating the default constructor?

the Java compiler.

If you don’t implement any constructor in your class, the Java compiler inserts default constructor into your code.

Of course both are same in result and you will not see the default constructor in your source code(the .java file).

And also my teacher told me it is better to add the constructor your self in your source code, even when you have no arguments!

What is the actual use of the default constructor in java?

The problem is that while you know the default constructor doesn't do anything in this example, in future the constructor might do something even if you don't realise it is and you might not be able to re-compile everywhere the code is used reliably. So the safest, simplest thing to do is to always call a constructor which might change in the future and let the JIT optimise away the constructor if it doesn't actually do anything.


The byte code always calls a contructor, whether you provide one or not. When you compile code which uses the default constructor it cannot assume the constructor doesn't do anything useful as you can add something to it later to do something useful. e.g.

Say you change

public class Test {
int a;

// public Test() { //this constructor does nothing
}

to

public class Test {
int a;
final List<String> strings = new ArrayList<>();

// public Test() { //this constructor does something now.
}

or

public class ServerTest {
final List<String> strings = new ArrayList<>();
}

public class Test extends SuperTest {
int a;
// the default constructor has to call super();
}

The constructor now initialised the strings field. You can change this class without having to re-compile everywhere it is used and say, hey, my constructor now does something useful, you should call it now.

Java using default constructor

Every class comes with a default constructor that is not visible in the class itself. However, please note that if you specify a constructor other than the default constructor, the default constructor can't be used, per @Rustam comment below. For example, let's say your Customer class looks like this:

public class Customer {

private String name;

private String lastName;

private age int;

private String ssn;

//default constructor that is NOT visible
Customer()
{

}

//other constructor given name and lastName
Customer(name, lastName)
{
this.name = name;

this.lastName = lastName;
}

//getters and setters

}

The constructor Customer() is created by default and it is not necessary for you to include in your class.

You can then create a customer instance using the default constructor and then you would need to use setters to set the properties, like follows:

public class Test {

public static void main(String [] args){

Customer c = new Customer();

//setting parameters
c.setName("Jose");

c.setLastName("Mejia");

}

}


Related Topics



Leave a reply



Submit