Skip to main content

Command Palette

Search for a command to run...

Introducing Classes

Published
โ€ข5 min readโ€ขView as Markdown
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 ๐Ÿ’ช