DEV Community

Cover image for πŸš€ Day 7 of My Automation Journey – Default Values & Constructors in Java
bala d kaveri
bala d kaveri

Posted on

πŸš€ Day 7 of My Automation Journey – Default Values & Constructors in Java

Today I learned two very important concepts in Java:

βœ… Default Values
βœ… Constructors
βœ… Object Initialization

This is where Java becomes more practical and object-oriented.

πŸ”Ή What Are Default Values in Java?

When we declare instance variables (class-level variables) and do not assign values, Java automatically assigns default values.

πŸ“Œ Default Values of Data Types

Data Type   Default Value
int                0
double               0.0
float                0.0f
boolean              false
char                 '\u0000' (empty character)
String               null
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Important: Default values are assigned only to instance variables, not local variables.

πŸ”Ή What Is a Constructor?

A constructor:
Has the same name as the class
Is automatically called when object is created
Has no return type
Is used to initialize object-specific values

Syntax

public ClassName(parameters) {
    // initialization
}
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Real Example – SuperMarket Program

Let’s understand the code step by step.

package java_Module_2_Constructor;

public class SuperMarket {

    String prod_name, Quantity;
    int price, discount;

    public SuperMarket(String prod_name, String Quantity, int price, int discount) {
        // TODO Auto-generated constructor stub
        this.prod_name = prod_name;
        this.Quantity = Quantity;
        this.price = price;
        this.discount = discount;
        // System.out.println(prod_name + Quantity + price + discount); 

    }

    public static void main(String[] args) {

        SuperMarket prod_1 = new SuperMarket("Pepsi ", "1/2 litre ", 35 , 5);
        SuperMarket prod_2 = new SuperMarket("Pepsi ", "1 litre ", 55 , 5 );
        SuperMarket prod_3 = new SuperMarket("Pepsi ", "1 1/2 litre ", 75 , 10);
        SuperMarket prod_4 = new SuperMarket("Pepsi ", "2 litre ", 95 , 15);    

        prod_1.sell();
        prod_2.sell();
        prod_3.sell();
        prod_4.sell();


    }

    private void sell() {
        // TODO Auto-generated method stub
        System.out.println(prod_name);
        System.out.println(Quantity);
        System.out.println(price);
        System.out.println(discount);
        System.out.println("-------------------");
    }

}
Enter fullscreen mode Exit fullscreen mode
Result:

Pepsi 
1/2 litre 
35
5
-------------------
Pepsi 
1 litre 
55
5
-------------------
Pepsi 
1 1/2 litre 
75
10
-------------------
Pepsi 
2 litre 
95
15
-------------------
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή What Is this Keyword?

this refers to the current object.

Example:

this.price = price;
Enter fullscreen mode Exit fullscreen mode

Left side β†’ instance variable
Right side β†’ constructor parameter

It removes confusion when variable names are same.

πŸ”Ή How Object Creation Works

When you write:

SuperMarket prod_1 = new SuperMarket("Pepsi", "1 litre", 55, 5);
Enter fullscreen mode Exit fullscreen mode

Step-by-step:

Memory is allocated
Default values are assigned
Constructor is called
Values are initialized
Object is ready to use

πŸ”Ή Why Constructor Is Important?

Without constructor:

Variables remain default (0, null, false)

No proper initialization

With constructor:

Each object can have different values

Real-world modeling becomes possible

🧠 Real-Life Understanding

Think of constructor like a form you fill when creating a product:

Product Name
Quantity
Price
Discount

Every product object will have its own data.

🎯 Interview Points

βœ” Constructor has no return type
βœ” Automatically called when object is created
βœ” Used to initialize instance variables
βœ” If not written, Java provides default constructor
βœ” this keyword refers to current object

πŸš€ Summary of Day 7

Today I learned:

Default values in Java
What is constructor
How object initialization works
Importance of this keyword
Real-world object modeling

Java is slowly becoming more powerful and meaningful in my automation journey πŸ”₯

Date: 26/02/2026
Trainer: Nantha from Payilagam

πŸ€– A Small Note
I used ChatGPT to help me structure and refine this blog.

Top comments (0)