Alex Rivera | Logout

Java default constructor

Asked 2010-12-20T10:24:16.010
198

What exactly is a default constructor — can you tell me which one of the following is a default constructor and what differentiates it from any other constructor?

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

public Module(String name, int credits, int hours) {
   this.name = name;
   this.credits = credits;
   this.hours = hours;
}
Edit
Report

2 Answers

43

A default constructor is created if you don't define any constructors in your class. It simply is a no argument constructor which does nothing. Edit: Except calling super().

public Module(){
}
answered 2010-12-20T10:27:27.777
-2

A default constructor does not take any arguments:

public class Student { 
    // default constructor
    public Student() {   

    }
}
answered 2010-12-20T10:34:07.290

Your Answer