The purpose of the constructor is to initialize an object.
#Java default constructor how to
This class however has one or more parameterized constructor and this results in a compile time error.įor example, let us create a class Student with a single parameterized constructor and try to instantiate it via the no-arg constructor. What is the definition of a constructor in Java That means you define a way how to initialize a new object instance. The problem that Ayush was facing was in trying to instantiate an Object for a class via a no-arg constructor. Java provides a default constructor which takes no arguments and performs no special actions or initializations, when no explicit constructors are provided. I will try to answer the question anew for new-comers. Default constructor provides the default values to the object like 0, null etc. So if you are creating an Object of a class it will. In case the class contains parametarized constructors. While all the answers above are correct, it's a bit difficult for new-comers to wrap it in their head. Color name:Red HtmlCode:FF0000 As you can see, we are able to instantiate object using at run time using reflection. Java does not initialize any local variable to any default value. The compiler provides a default constructor if no other constructors are available in the class.