> For the complete documentation index, see [llms.txt](https://www.pranaypourkar.co.in/the-programmers-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.pranaypourkar.co.in/the-programmers-guide/java/java-basics/oop-basics/what-is-a-class.md).

# What is a Class?

## About

A **class** in Java is a **blueprint** or **template** for creating objects. It defines the **state (fields/attributes)** and **behavior (methods)** that objects of that class will have.

## **Characteristics of a Class**

### **1. Defines State (Attributes)**&#x20;

* Represented by **fields/variables** inside the class.
* Example: `String name;`, `int age;`.

### **2. Defines Behavior (Methods)**&#x20;

* Defined by functions inside the class.
* Example: `void eat() {}`, `void sleep() {}`.

### **3. Can Contain a Constructor**&#x20;

* Special method used to **initialize** an object.
* Example: `Person(String name, int age) { this.name = name; this.age = age; }`.

### **4. Objects are Created from a Class**&#x20;

* A class itself is just a definition; actual **objects** are instantiated from it.

## **Example of a Class in Java**

```java
// Class Definition (Blueprint)
class Person {
    // Attributes (State)
    String name;
    int age;

    // Constructor (Used to initialize object)
    Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Methods (Behavior)
    void introduce() {
        System.out.println("Hi, I am " + name + " and I am " + age + " years old.");
    }
}

// Main Class
public class Main {
    public static void main(String[] args) {
        // Creating Objects from Class
        Person person1 = new Person("Alice", 25);
        Person person2 = new Person("Bob", 30);

        // Calling Methods
        person1.introduce(); // Hi, I am Alice and I am 25 years old.
        person2.introduce(); // Hi, I am Bob and I am 30 years old.
    }
}
```

## **How Classes Work in Memory**

* The **class definition** itself is stored in **method area (Metaspace in Java 8+)**.
* When an **object** is created, memory is allocated in the **heap**, and the reference variable (like `person1`) is stored in the **stack**.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://www.pranaypourkar.co.in/the-programmers-guide/java/java-basics/oop-basics/what-is-a-class.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
