Skip to main content
Java Basics
CHAPTER 13 Beginner

Classes and Objects

Updated: May 17, 2026
5 min read

# CHAPTER 13

Classes and Objects

1. Introduction

Classes and objects are the fundamental building blocks of Java. A class defines the structure; an object brings it to life. In this chapter, we'll build real-world classes with attributes, methods, and access control.

2. Learning Objectives

  • Create classes with attributes and methods.
  • Instantiate objects using new.
  • Use access modifiers (public, private, protected, default).
  • Understand static vs instance members.
  • Use the this keyword.

3. Creating a Class

java
12345678910111213
public class Student {
    // Attributes (fields)
    String name;
    int age;
    double gpa;
    
    // Method
    void displayInfo() {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("GPA: " + gpa);
    }
}

4. Creating Objects

java
123456789101112131415
public class Main {
    public static void main(String[] args) {
        Student s1 = new Student();
        s1.name = "Alice";
        s1.age = 20;
        s1.gpa = 3.8;
        s1.displayInfo();
        
        Student s2 = new Student();
        s2.name = "Bob";
        s2.age = 22;
        s2.gpa = 3.5;
        s2.displayInfo();
    }
}
12345678910111213
Memory Model:
Stack:                  Heap:
+------+               +------------------+
| s1   | ------------> | name = "Alice"   |
+------+               | age = 20         |
| s2   | -----+        | gpa = 3.8        |
+------+      |        +------------------+
              |
              +-------> +------------------+
                        | name = "Bob"     |
                        | age = 22         |
                        | gpa = 3.5        |
                        +------------------+

5. Access Modifiers

ModifierClassPackageSubclassWorld
public
protected
(default)
private

6. The this Keyword

Refers to the current object instance.
java
123456789
public class Person {
    String name;
    int age;
    
    void setDetails(String name, int age) {
        this.name = name;   // 'this.name' is the field
        this.age = age;     // 'name' is the parameter
    }
}

7. Static vs Instance Members

java
123456789
public class Counter {
    static int totalCount = 0;  // Shared across ALL objects
    int instanceCount = 0;       // Unique to EACH object
    
    Counter() {
        totalCount++;
        instanceCount++;
    }
}
  • Static: Belongs to the class. One copy shared.
  • Instance: Belongs to each object. Each has its own copy.

8. Mini Project: Employee Management System

java
1234567891011121314151617181920212223242526272829303132
public class Employee {
    private String name;
    private String department;
    private double salary;
    private static int totalEmployees = 0;

    public Employee(String name, String department, double salary) {
        this.name = name;
        this.department = department;
        this.salary = salary;
        totalEmployees++;
    }

    public void displayInfo() {
        System.out.printf("Name: %s | Dept: %s | Salary: $%.2f%n", name, department, salary);
    }

    public static int getTotalEmployees() {
        return totalEmployees;
    }

    public static void main(String[] args) {
        Employee e1 = new Employee("Alice", "Engineering", 95000);
        Employee e2 = new Employee("Bob", "Marketing", 72000);
        Employee e3 = new Employee("Charlie", "Engineering", 105000);

        e1.displayInfo();
        e2.displayInfo();
        e3.displayInfo();
        System.out.println("Total Employees: " + Employee.getTotalEmployees());
    }
}

9. MCQ Quiz with Answers

Question 1

Which keyword creates an object?

Question 2

private fields are accessible from:

Question 3

this refers to:

Question 4

Static variables are shared by:

Question 5

Default access modifier allows access from:

Question 6

Object references are stored on:

Question 7

Object data is stored on:

Question 8

How many public classes can a .java file have?

Question 9

Static methods can access:

Question 10

this() calls:

10. Interview Questions

  • Q: What is the difference between a class and an object?
  • Q: Explain the difference between static and instance variables.
  • Q: Why is this keyword needed?
  • Q: Can a static method access instance variables? Why or why not?

11. Summary

Classes define the blueprint with attributes and methods. Objects are instances created with new. Access modifiers control visibility. static members belong to the class; instance members belong to each object. The this keyword resolves ambiguity between fields and parameters.

12. Next Chapter Recommendation

In Chapter 14: Constructors in Java, we'll learn how to properly initialize objects using constructors — default, parameterized, and overloaded.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·