Introducing Classes

๐ง One Golden Rule of Java
Before learning anything else, remember this:
In Java, everything happens inside a class.
You cannot write Java code outside a class.
So to understand Java, we must first understand what a class is.
๐ What Is a Class? (Real-Life Example)
Think of it like this ๐
Class โ House plan (blueprint)
Object โ Real house built using that plan
๐ A plan is not a house
๐ A class is not an object
A class only describes something.
An object is the real thing created from it.
โ A Very Simple Class Example
class Box {
double width;
double height;
double depth;
}
Donโt panic ๐
Letโs read this in plain English:
โBox is a type.
Every Box has width, height and depth.โ
โ This code does not create a box
โ It only tells Java what a box looks like
๐ฆ What Is an Object?
An object is a real instance created from a class.
Box mybox = new Box();
Read it slowly ๐
โCreate a new Box and name it mybox.โ
Now mybox is a real box stored in memory.
๐งฉ Why Do We Need Objects?
Because only objects:
Store values
Occupy memory
Do real work
๐ Class = idea
๐ Object = reality
๐ Accessing Data Using Dot (.)
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;
Read like English:
โWidth of mybox is 10.โ
The dot (.) means:
๐ belongs to
๐งฎ A Complete Very Simple Program
class Box {
double width;
double height;
double depth;
}
class BoxDemo {
public static void main(String[] args) {
Box mybox = new Box();
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;
double volume = mybox.width * mybox.height * mybox.depth;
System.out.println("Volume is " + volume);
}
}
Output:
Volume is 3000.0
๐ This is core Java.
Take your time here โ donโt rush.
๐ง Important Beginner Rule
Each object has its own data.
Box b1 = new Box();
Box b2 = new Box();
โ b1 and b2 are different boxes
โ Changing b1 does NOT affect b2
๐ช Creating an Object Happens in Two Steps
Step 1: Create reference
Box b;
๐ No object yet.
Step 2: Create object
b = new Box();
Usually written together:
Box b = new Box();
๐งจ What Does new Mean?
new Box();
Means:
โHey Java, give me memory for a Box.โ
Java:
Allocates memory
Returns the address
Stores it in the reference variable
โ A Very Common Beginner Mistake
Box b1 = new Box();
Box b2 = b1;
โ No new box is created
โ Both variables point to the same box
Think of it as:
๐ Two names for one object
๐ What Is a Method?
A method is a block of code that does some work.
void sayHello() {
System.out.println("Hello");
}
Methods help us:
Reuse code
Keep programs clean
Add behavior to objects
๐งฎ Method Inside a Class
class Box {
double width, height, depth;
double volume() {
return width * height * depth;
}
}
Now the box knows how to calculate its own volume.
๐ Calling a Method
Box b = new Box();
b.width = 10;
b.height = 20;
b.depth = 15;
System.out.println(b.volume());
The dot means:
โCall volume() of b.โ
๐ Understanding return
double volume() {
return width * height * depth;
}
return means:
โSend the result back.โ
๐ฅ Method with Parameters
int square(int x) {
return x * x;
}
Calling the method:
square(5); // 25
square(10); // 100
๐ x โ parameter
๐ 5, 10 โ arguments
โ Bad Way to Set Object Data
b.width = 10;
b.height = 20;
b.depth = 15;
Problems:
Easy to forget a value
Poor design
โ Better Way: Use a Method
void setDim(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
Use it like this:
b.setDim(10, 20, 15);
โ Cleaner
โ Safer
โ Professional approach
๐ Constructor (VERY IMPORTANT)
A constructor:
Runs automatically
When an object is created
Used to set initial values
class Box {
double width, height, depth;
Box() {
width = 10;
height = 10;
depth = 10;
}
}
This runs automatically:
Box b = new Box();
๐ข Constructor with Values
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
Use:
Box b = new Box(10, 20, 15);
โ Clean
โ Safe
โ Interview-ready
๐ this Keyword (Very Simple)
this means:
๐ current object
Box(double width, double height, double depth) {
this.width = width;
this.height = height;
this.depth = depth;
}
Left side โ object variable
Right side โ parameter
๐ Garbage Collection (No Fear)
Java automatically removes unused objects.
โ You donโt delete objects
โ Java manages memory for you
โค๏ธ Mentor Advice for Beginners
If you are new to Java:
1๏ธโฃ Read slowly
2๏ธโฃ Type the code yourself
3๏ธโฃ Understand โ donโt memorize
4๏ธโฃ Practice one concept at a time
๐ฏ Final Thoughts
Classes and objects are the foundation of Java.
Once this chapter is clear, everything else becomes easier.
Youโre doing great โ keep going ๐ช