Lesson 32 of 50 – Java Classes
64%

Java Classes

A class is one of the fundamental concepts of Java. It defines the properties and behaviors that objects created from that class can have.

1. What Is a Class in Java?

A class is a blueprint or template used to create objects. It can contain variables, constructors, and methods.

For example, a Student class can contain:

  • Name of the student
  • Age of the student
  • Method to display student details
class Student {
    String name;
    int age;

    void display() {
        System.out.println(name);
        System.out.println(age);
    }
}
Remember: A class defines the structure and behavior that its objects can have.
2. Syntax of a Java Class

The general syntax of a class is:

class ClassName {

    // Fields or variables

    // Constructors

    // Methods
}

Example:

class Car {
    String color;
    int speed;

    void start() {
        System.out.println("Car is starting");
    }
}

Here, Car is the class name, color and speed are fields, and start() is a method.

3. Creating an Object from a Class

An object is an instance of a class. We commonly create an object using the new keyword.

class Student {
    String name = "Rahul";

    void display() {
        System.out.println(name);
    }

    public static void main(String[] args) {
        Student s1 = new Student();

        s1.display();
    }
}
Output:
Rahul

Explanation:

  • Student is the class.
  • s1 is a reference variable.
  • new Student() creates an object.
  • s1.display() calls the method.
4. Class Variables (Fields)

Variables declared inside a class but outside its methods are called fields or member variables.

class Employee {
    String name = "Amit";
    int salary = 25000;

    public static void main(String[] args) {
        Employee e1 = new Employee();

        System.out.println(e1.name);
        System.out.println(e1.salary);
    }
}
Output:
Amit
25000

Non-static fields belong to individual objects. Each object can have its own field values.

5. Methods Inside a Class

A method defines an action or behavior that an object can perform.

class Calculator {

    int add(int a, int b) {
        return a + b;
    }

    public static void main(String[] args) {
        Calculator c1 = new Calculator();

        int result = c1.add(10, 20);

        System.out.println(result);
    }
}
Output:
30

The add() method accepts two integers and returns their sum.

6. Creating Multiple Objects

We can create multiple objects from the same class. Each object can store different instance field values.

class Student {
    String name;

    public static void main(String[] args) {
        Student s1 = new Student();
        Student s2 = new Student();

        s1.name = "Rahul";
        s2.name = "Priya";

        System.out.println(s1.name);
        System.out.println(s2.name);
    }
}
Output:
Rahul
Priya
Each object has its own copy of non-static instance fields.
7. Class Members: Static and Instance

Java classes can contain both static and instance members.

Static Member Instance Member
Associated with the class. Associated with each object.
Accessed using the class name when accessible. Usually accessed through an object.
One class-level field is shared. Each object has its own instance field.
class Demo {
    static String college = "ABC College";
    String studentName;

    public static void main(String[] args) {
        Demo d1 = new Demo();
        Demo d2 = new Demo();

        d1.studentName = "Rahul";
        d2.studentName = "Priya";

        System.out.println(Demo.college);
        System.out.println(d1.studentName);
        System.out.println(d2.studentName);
    }
}
Output:
ABC College
Rahul
Priya
8. Accessing Class Members

Members of a class can be accessed according to their access modifiers and whether they are static or instance members.

class Person {
    String name = "Amit";

    void introduce() {
        System.out.println("My name is " + name);
    }

    public static void main(String[] args) {
        Person p1 = new Person();

        System.out.println(p1.name);
        p1.introduce();
    }
}
Output:
Amit
My name is Amit

The dot operator (.) is used to access an object's accessible fields and methods.

9. The Relationship Between Classes and Objects

Consider this simple example:

class Mobile {
    String brand;
    int price;
}

Mobile is a class. We can create objects representing different mobile phones.

Mobile m1 = new Mobile();
m1.brand = "Samsung";
m1.price = 20000;

Mobile m2 = new Mobile();
m2.brand = "Apple";
m2.price = 50000;
Object Brand Price
m1 Samsung 20000
m2 Apple 50000
10. Access Modifiers in Classes

Access modifiers control where classes and their members can be accessed.

Modifier Meaning
public Accessible wherever Java access rules permit.
private Accessible within the declaring top-level class.
protected Accessible in the same package and in qualifying subclasses.
No modifier Package-private access.

A top-level class can generally be declared public or package-private, not private or protected.

11. A Class with a Constructor

A constructor initializes a newly created object. It has the same name as the class and has no return type.

class Student {
    String name;
    int age;

    Student(String studentName, int studentAge) {
        name = studentName;
        age = studentAge;
    }

    void display() {
        System.out.println(name);
        System.out.println(age);
    }

    public static void main(String[] args) {
        Student s1 = new Student("Rahul", 20);
        s1.display();
    }
}
Output:
Rahul
20

The constructor receives values when the object is created and assigns them to the instance fields.

12. Real-Life Example: Bank Account
class BankAccount {
    String accountHolder;
    double balance;

    BankAccount(String name, double initialBalance) {
        accountHolder = name;
        balance = initialBalance;
    }

    void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
        }
    }

    void displayBalance() {
        System.out.println("Account Holder: " + accountHolder);
        System.out.println("Balance: " + balance);
    }

    public static void main(String[] args) {
        BankAccount account =
            new BankAccount("Rahul", 1000);

        account.deposit(500);
        account.displayBalance();
    }
}
Output:
Account Holder: Rahul
Balance: 1500.0

This example combines fields, a constructor, methods, and an object in one class.

13. Practice Assignments
  1. Create a Student class with name, age, and a method to display student details.
  2. Create an Employee class with name, salary, and department.
  3. Create a Car class with brand, color, and a method named start().
  4. Create two objects of a Mobile class and assign different brand names and prices.
  5. Create a Rectangle class with length and width fields and a method to calculate area.
  6. Create a Book class with a constructor that initializes the title and author.
  7. Create a BankAccount class with deposit and balance-display methods.
Practice Tip: Create each program in a file matching the public class name, such as Student.java for a public Student class.

🧠 Quick Quiz

1. What is a class in Java?




2. Which keyword is commonly used to create an object?




3. What is a constructor used for?