Java Reference Library →

class Keyword Usage and Examples

class Keyword Usage and Examples

Learn how the Java class keyword is used to define classes in Java. Understand its syntax, purpose, and real-world examples to build your object-oriented programming foundation.

This reference explains class Keyword Usage and Examples through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.

  1. Use this reference

    Identify the documented behavior, read a complete example and check its inputs before applying it.

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.

Using this reference

Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.

Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.

Supporting documentation: Official Java learning guide.

Reference checks are optional and do not count toward course progress.

    Reference library →