DEV Community

Cover image for Constructors, Default Values & Object Initialization in Java
scindia bethuraj
scindia bethuraj

Posted on

Constructors, Default Values & Object Initialization in Java

Trainer: Mr.Nantha
Day-7

Overview

Today’s session focused on three important topics in Java: default values, constructors, and object initialization.

Constructors

Constructors are special methods used to create and initialize objects in Java. They allow developers to set up an object’s initial state when it is instantiated.

Important Rules

(1) Constructor name must be same as class name.

(2) It does not have a return type (not even void).

(3) It runs automatically when object is created.

Example
class Student {

String name;

// Constructor
Student(String n) {
    name = n;
}

public static void main(String[] args) {
    Student s1 = new Student("Arjun");
    System.out.println(s1.name);
}
Enter fullscreen mode Exit fullscreen mode

}

Output:

Arjun

What Happens Here?
Student s1 = new Student("Arjun");

✔ Memory is created in Heap
✔ Constructor runs automatically
✔ Variable name gets initialized

Default Values in Java

When we create an object, Java automatically assigns default values to instance variables if we do not initialize them.

Default Values for Instance Variables:

|Data Type| |Default Value|

|int| |0|
|double| |0.0|
|float| |0.0f|
|boolean| |false|
|char| |'\u0000' (empty character)|
|String (or any object)| |null|

Default Values

Default values refer to the initial values assigned to fields or variables when an object is created, especially if no explicit value is provided.

Object Initialization

Object initialization is the process that occurs when an object is created, involving the assignment of default values and the execution of constructors to prepare the object for use.

Default Values vs Constructor Initialization

  |Default Values|                          |Constructor|
Enter fullscreen mode Exit fullscreen mode

|Assigned automatically by Java| |Assigned by programmer|
|Only for instance variables| |Used to set custom values|
|Cannot control value| |Can control initialization|

Object Initialization in Java

Object Initialization means assigning values to the variables of an object when it is created.

When we create an object using the new keyword, memory is allocated in the Heap. Then the object’s variables must be initialized with values.

Why Object Initialization is Important?

Prevents null or default value issues

Makes object ready to use

Improves code clarity

Example: Which I practiced in class today for Constructor.

package Constructor;

public class School {

    int id;

    //Constructor
    School(int stdId)
    {
        this.id = stdId;
    }



    public static void main(String[] args) {

        School yogesh = new School (101);
        System.out.println(yogesh.id);

    }
}   
Enter fullscreen mode Exit fullscreen mode

Output:
101

Key Takeaways

• Constructors are special methods that are used to create and initialize objects, allowing developers to set up the initial state of an object upon instantiation.

• Default values are the initial values assigned to fields or variables in Java when no explicit value is provided.

• Object initialization involves both the assignment of default values and the execution of constructors, ensuring that objects are properly prepared for use in Java programs.

Top comments (0)