Help the world stop coronavirus! Stay home!

Prev Next

Java / Constructor

Purpose of default constructor.

Default constructor initializes the instance variables to its default values if not initalized explicitly depending on the type.

For objects, default constructor initializes it to null, int with 0.

In the below example, make String variable is initialized to null, numberofSeats to 0 and isItOldModel to false.

public class Car {
	String make;

	int numberofSeats;

	boolean isItOldModel;

	public String toString() {

		return "Car make is " + make + " and it has " + numberofSeats
				+ " seats and is it old model? " + isItOldModel + ".";

	public static void main(String[] args) {

		Car newCar = new Car();




Car make is null and it has 0 seats and is it old model? false.

❤Cash Back At Stores you Love !!!❤

Earn your $10 reward when you make your first purchase through Ebates by signing up with clicking below button.

Ebates Coupons and Cash Back

More Related questions...

What is a Constructor? Rules for creating a Java constructor. Types of Java constructor. Java Default Constructor. Java parameterized constructor. Advantages of parameterized constructor. What happens when the developer does not implement default constructor? Purpose of default constructor. Constructor Overloading in Java. Copy-Constructor. Difference between constructor and method in Java. What are pass by reference and pass by value? Can constructor perform other tasks instead of initialization? Can a constructor call/invoke a static method? Can a constructor invoke a non-static/instance method? Can abstract class have constructors? What is the access modifier of the Default constructor? Can the class with private constructor be extended? Can the inner class with private constructor be extended? Can you use this() and super() both together in a constructor? Can constructor be synchronized in Java? Is constructor inherited? Can a constructor be final? Can we call the constructor of a class more than once for an object in Java? Is constructor called when cloning in Java? Different ways to create an object in Java. Difference between Class.newInstance() and Constructor.newInstance(). How to Initialize class with Class.forName() that has parameterized constructor? Can you define a method with the same name as the class? What happens when we add a return type to the constructor? Is Constructor definition mandatory? If a class has an explicit constructor, does compiler create default constructor? Can we throw exceptions from a constructor? What are the access modifiers that cannot be applied to a constructor? What is constructor chaining? How do I call subclass constructor from superclass constructor? What is No-arg constructor or default constructor.? Can you declare Constructor as final in Java?
Show more question and Answers...


Comments & Discussions