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
π 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
}
πΉ 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("-------------------");
}
}
Result:
Pepsi
1/2 litre
35
5
-------------------
Pepsi
1 litre
55
5
-------------------
Pepsi
1 1/2 litre
75
10
-------------------
Pepsi
2 litre
95
15
-------------------
πΉ What Is this Keyword?
this refers to the current object.
Example:
this.price = price;
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);
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)