# 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

```java
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.

```java
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 (.)

```java
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

```java
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:

```java
Volume is 3000.0
```

📌 This is **core Java**.  
Take your time here — don’t rush.

---

## 🧠 Important Beginner Rule

Each object has **its own data**.

```java
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

```java
Box b;
```

👉 No object yet.

### Step 2: Create object

```java
b = new Box();
```

Usually written together:

```java
Box b = new Box();
```

---

## 🧨 What Does `new` Mean?

```java
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

```java
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.

```java
void sayHello() {
    System.out.println("Hello");
}
```

Methods help us:

* Reuse code
    
* Keep programs clean
    
* Add behavior to objects
    

---

## 🧮 Method Inside a Class

```java
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

```java
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`

```java
double volume() {
    return width * height * depth;
}
```

`return` means:

> “Send the result back.”

---

## 📥 Method with Parameters

```java
int square(int x) {
    return x * x;
}
```

Calling the method:

```java
square(5);   // 25
square(10);  // 100
```

📌 `x` → parameter  
📌 `5`, `10` → arguments

---

## ❌ Bad Way to Set Object Data

```java
b.width = 10;
b.height = 20;
b.depth = 15;
```

Problems:

* Easy to forget a value
    
* Poor design
    

---

## ✅ Better Way: Use a Method

```java
void setDim(double w, double h, double d) {
    width = w;
    height = h;
    depth = d;
}
```

Use it like this:

```java
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
    

```java
class Box {
    double width, height, depth;

    Box() {
        width = 10;
        height = 10;
        depth = 10;
    }
}
```

This runs automatically:

```java
Box b = new Box();
```

---

## 🔢 Constructor with Values

```java
Box(double w, double h, double d) {
    width = w;
    height = h;
    depth = d;
}
```

Use:

```java
Box b = new Box(10, 20, 15);
```

✔ Clean  
✔ Safe  
✔ Interview-ready

---

## 🔁 `this` Keyword (Very Simple)

`this` means:  
👉 **current object**

```java
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 💪
