Stop 04 · Classes & ObjectsTopics · 1/8

Classes & Objects

Part of the Java Roadmap

In short

Java is built around objects. A Swiggy order, a bank account, a student: each is modelled as an object with its own data and actions. Learn classes and objects and the rest of Java starts making sense.

A class defines fields (state) and methods (behaviour), and objects are instances created with new. Each object has its own copy of instance fields.

The jaadu holds up a big blue paper floor plan in front of three small identical houses, each painted a different colour.

Think of it like this

The architect's flat plan

A builder in Noida has one floor plan for a 2BHK flat and builds two hundred flats from it. The plan is not a home; nobody lives in it. Each flat is real, with its own family, furniture and paint colour. A class is the plan, and each object made with new is one real flat.

Watch it happen

One class, many objects

class Student
riya: name=Riya, cgpa=8.9
arjun: name=Arjun, cgpa=7.6
meera: name=Meera, cgpa=9.2

1/4The class declares fields (name, cgpa) and methods (isTopper). No real student exists yet.

The big ideas

What you need to know

Class = blueprint

Defines what every object will have (fields) and can do (methods).

Object = instance

Created at runtime with new. Each has its own copy of the instance fields.

Dot to reach inside

riya.name reads a field and riya.isTopper() calls a method on that particular object.

Default values

Fields you do not set start as 0, false or null. Local variables, by contrast, have no default.

See it in code

Walk through the code

java
1public class Student {2    String name;3    double cgpa;4 5    boolean isTopper() {6        return cgpa >= 9.0;7    }8 9    void introduce() {10        System.out.println("I am " + name + ", CGPA " + cgpa);11    }12 13    public static void main(String[] args) {14        Student riya = new Student();15        riya.name = "Riya";16        riya.cgpa = 8.9;17 18        Student meera = new Student();19        meera.name = "Meera";20        meera.cgpa = 9.2;21 22        riya.introduce();23        meera.introduce();24        System.out.println(meera.isTopper()); // true25    }26}

1/4Instance fields: every Student object gets its own name and cgpa.

One Student class and two objects with their own data. Next lessons replace the direct field writes with constructors and setters.

Save yourself the pain

Common mistakes

Oops

Calling an instance method from main like isTopper(); without an object.

Do this instead

main is static, so there is no current object. Create one and call riya.isTopper().

Oops

Thinking a class and an object are the same thing.

Do this instead

The class is written once in code. Objects are created as many times as you call new.

Your mission

Model a mobile phone

~20 min

Quick check

What does new Student() do?

Go deeper

  1. 1Create a Student class with fields and a method, then create several objects.
  2. 2Draw how references and objects live in memory.
  3. 3Override toString() so printing an object shows useful information.

Also part of these roadmaps

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge