Java class Keyword
Usage and Examples

class Keyword in Java

The class keyword in Java is one of the fundamental building blocks of the language. It is used to define a blueprint for objects. Think of it as a mold — it doesn't hold values itself, but it tells Java what an object of that class should contain and be able to do.

What is a Class in Java?

A class in Java is a user-defined data type that encapsulates data (fields) and operations (methods). It represents real-world entities like Student, Car, or BankAccount. The class keyword is how you start defining one.

Syntax of the class Keyword

class ClassName {
    // fields
    // methods
}

Note: The name of the class should follow Java naming conventions — typically written in PascalCase.

Basic Example: Creating a Simple Class

class Dog {
    String breed;
    int age;

    void bark() {
        System.out.println("Woof!");
    }
}

This Dog class defines two fields (breed and age) and one method (bark()).

Using the Class in a Java Program

Defining a class alone does not execute anything. You must create an object from it and use its members:

public class Main {
    public static void main(String[] args) {
        Dog myDog = new Dog(); // creating an object of Dog
        myDog.breed = "Labrador";
        myDog.age = 3;

        System.out.println("Breed: " + myDog.breed);
        System.out.println("Age: " + myDog.age);
        myDog.bark();
    }
}
Breed: Labrador
Age: 3
Woof!

Important Concepts Around the class Keyword

1. Class vs. Object

The class defines the structure. An object is an actual instance of that structure. One class can be used to create multiple objects.

2. Class with Constructor

You can add a constructor to initialize object values at the time of creation:

class Student {
    String name;
    int rollNo;

    Student(String n, int r) {
        name = n;
        rollNo = r;
    }

    void display() {
        System.out.println("Name: " + name + ", Roll No: " + rollNo);
    }
}
public class Main {
    public static void main(String[] args) {
        Student s1 = new Student("Alice", 101);
        s1.display();
    }
}
Name: Alice, Roll No: 101

3. Public, Default, and Final Classes

  • public class: Can be accessed from any other class.
  • default class (no modifier): Accessible only within its package.
  • final class: Cannot be extended (inherited).
final class Utility {
    // cannot be extended
}

4. Nested Classes

You can define a class inside another class. These are called nested or inner classes.

class Outer {
    class Inner {
        void sayHello() {
            System.out.println("Hello from Inner class!");
        }
    }
}
public class Main {
    public static void main(String[] args) {
        Outer outer = new Outer();
        Outer.Inner inner = outer.new Inner();
        inner.sayHello();
    }
}
Hello from Inner class!

Best Practices When Using class

  • Use meaningful names that reflect the class's purpose (e.g., Customer, Invoice).
  • Encapsulate fields using private and expose them through getters and setters.
  • Keep one public class per file (recommended).
  • Follow naming conventions: class names should begin with uppercase letters.