0
0
Javaprogramming~10 mins

Default constructor in Java - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Default constructor
Class declared
No constructor written?
Yes
Compiler adds default constructor
Object created using default constructor
Object initialized with default values
Program continues
When no constructor is written, Java adds a default constructor automatically to create objects with default values.
Execution Sample
Java
class Car {
  int speed;

  public static void main(String[] args) {
    Car c = new Car();
    System.out.println(c.speed);
  }
}
Creates a Car object using the default constructor and prints the default speed value.
Execution Table
StepActionEvaluationResult
1Class Car declared with int speedNo constructor definedCompiler will add default constructor
2Create object c with new Car()Calls default constructorObject c created, speed initialized to 0
3Print c.speedspeed is 0 by defaultOutput: 0
4Program endsNo more actionsExecution stops
💡 Program ends after printing default value of speed
Variable Tracker
VariableStartAfter Object CreationFinal
c.speedundefined00
Key Moments - 2 Insights
Why can we create an object without writing a constructor?
Because Java automatically adds a default constructor if none is written, as shown in execution_table step 1 and 2.
What value does the speed variable have after creating the object?
It has the default value 0 for int type, as shown in execution_table step 3 and variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 2?
AThe program prints the speed value
BThe default constructor is called and object c is created
CThe speed variable is set to 10
DThe class Car is deleted
💡 Hint
Check execution_table row with Step 2 describing object creation
According to variable_tracker, what is the value of c.speed after object creation?
Aundefined
Bnull
C0
D1
💡 Hint
Look at variable_tracker row for c.speed after object creation
If we add a constructor with parameters, what happens to the default constructor?
AIt is removed and not added automatically
BIt is still added automatically
CIt runs twice
DThe program crashes
💡 Hint
Recall that default constructor is only added if no constructor is written (see concept_flow)
Concept Snapshot
Default constructor in Java:
- Automatically added if no constructor is defined
- Has no parameters
- Initializes object with default values
- Allows object creation without explicit constructor
- If any constructor is written, default is NOT added
Full Transcript
In Java, when you create a class without writing any constructor, the compiler automatically adds a default constructor. This default constructor has no parameters and initializes the object with default values like 0 for int. For example, if you have a class Car with an int speed variable and no constructor, creating a new Car object calls this default constructor. The speed variable is set to 0 by default. This allows you to create objects easily without writing constructors. However, if you write any constructor yourself, the default constructor is not added automatically.