Explain Java Default Constructor

In This Tutorial, we will learn Java Default Constructor where the default constructor is a constructor which has just the name same as the name of the class without any argument and which is called on its own just by creating an object implicitly.

Java

Java Constructor

constructors are function that has the same name as the class name and we can give the data types even we can give the arguments and we can also overload the constructor. And we need to give an argument during creating the object and call the constructor.

The are being designed so that we can call the construction without calling it as they are called implicitly just by creating the object of class.

Default Constructor

A default constructor is a constructor which does not have any argument it is simply just the name of the class along with some data type. For example

public class Student {
  String firstName;
  String lastName;
  int age;

  //Student constructor
  public Student(){
      firstName = "Ihechikara";
      lastName = "Abba";
      age = 100;
  }
  
  public static void main(String args[]) {
      Student myStudent = new Student();
      System.out.println(myStudent.age);
      // 100
  }

Parameterized Constructor

It is a constructor where we provide some arguments along with data type and give parameters during creating the objects and providing the arguments so that we can provide the input to the function and return the output as we want.

public class Main {
  int modelYear;
  String modelName;

  public Main(int year, String name) {
    modelYear = year;
    modelName = name;
  }

  public static void main(String[] args) {
    Main myCar = new Main(1969, "Mustang");
    System.out.println(myCar.modelYear + " " + myCar.modelName);
  }
}

we can see that we provided the input during the creation of the object and called it implicitly with Object.

To learn more about Explain Java Default Constructor at Default Constructor

And to learn and solve all the java related problems at Java Tutorials faced learning java learning and get solved and also visit Python’s list: What is the difference Between methods Append and Extend? So solve python related problems also problems related to programming languages.

Leave a Comment

%d bloggers like this: